Skip to main content
In this guide, you’ll learn how to set up a code coverage workflow in GitHub Actions by combining a job container with a MongoDB service container. This approach ensures all steps run in a consistent environment and can connect to your database service seamlessly.

1. Recap: Unit Testing with a Service Container

Below is a sample job that runs unit tests in Node.js while using a MongoDB service container:

2. What Is a Job Container?

A job container allows you to execute every step of a job inside a specified Docker image instead of on the default runner VM. This ensures your CI environment matches production more closely.
The image shows a GitHub Docs page about "Running jobs in a container" using GitHub Actions, with an overview and example YAML code.
(See: https://docs.github.com/en/actions/using-jobs/running-jobs-in-a-container)
By specifying container.image, all subsequent steps run inside that Docker image. You no longer need a separate setup action for Node.js.

3. Defining the Code Coverage Workflow

The following workflow runs a code coverage job in a Node.js 18 container and attaches a MongoDB service container for database operations:
Make sure the service container name (mongo) matches the hostname used in MONGO_URI. Incorrect naming can lead to connection failures.

4. Verify and Inspect Logs

After committing and pushing your workflow, navigate to the Actions tab in GitHub. You should see both the Unit Testing and Code Coverage jobs running in parallel.
The image shows a GitHub Actions workflow interface with jobs for unit testing and code coverage in progress. It includes a visual representation of the workflow steps and their status.
The image shows a GitHub Actions workflow interface with a list of jobs and steps for code coverage, indicating successful completion of tasks like setting up a job, initializing containers, and checking out the repository.
In the logs, you’ll find Docker commands demonstrating how GitHub Actions starts both the job and service containers:

Watch Video