Skip to main content

Quickstart

Run unit tests

The test stage of the pipeline runs the application's unit tests and depends on the build-env stage. Let's look at its Dagger Function next.

Inspect the Dagger Function

package main

import (
"context"

"dagger/hello-dagger/internal/dagger"
)

type HelloDagger struct{}

// Return the result of running unit tests
func (m *HelloDagger) Test(ctx context.Context, source *dagger.Directory) (string, error) {
// get the build environment container
// by calling another Dagger Function
return m.BuildEnv(source).
// call the test runner
WithExec([]string{"npm", "run", "test:unit", "run"}).
// capture and return the command output
Stdout(ctx)
}

This Dagger Function works merely as a CLI wrapper. It starts with the Container result of the previous Dagger Function, executes the npm run test:unit run command in the container, and captures and returns the output as a string (refer to the code comments for details).

Call the Dagger Function

Call the Dagger Function as below:

dagger call test --source=.

Here's what you should see:

Test

SANDBOXING

All the Dagger Functions in this quickstart receive the location of the source code directory as a function argument, rather than reading it directly from the host filesystem. This is by design: Dagger Functions are fully "sandboxed" and do not have direct access to the host system. Therefore, host resources such as directories, files, environment variables, network services and so on must be explicitly passed to Dagger Functions as arguments. This "sandboxing" of Dagger Functions improves security, ensures reproducibility, and assists caching.