Home Explore Blog CI



docker

1st chunk of `content/guides/nodejs/run-tests.md`
d5a5438a36884b32ee48abb42d86af6f125b01280ac3c95c0000000100000a20
---
title: Run Node.js tests in a container
linkTitle: Run your tests
weight: 30
keywords: node.js, node, test
description: Learn how to run your Node.js tests in a container.
aliases:
  - /language/nodejs/run-tests/
  - /guides/language/nodejs/run-tests/
---

## Prerequisites

Complete all the previous sections of this guide, starting with [Containerize a Node.js application](containerize.md).

## Overview

Testing is an essential part of modern software development. Testing can mean a
lot of things to different development teams. There are unit tests, integration
tests and end-to-end testing. In this guide you take a look at running your unit
tests in Docker when developing and when building.

## Run tests when developing locally

The sample application already has the Jest package for running tests and has tests inside the `spec` directory. When developing locally, you can use Compose to run your tests.

Run the following command to run the test script from the `package.json` file inside a container.

```console
$ docker compose run server npm run test
```

To learn more about the command, see [docker compose run](/reference/cli/docker/compose/run/).

You should see output like the following.

```console
> docker-nodejs@1.0.0 test
> jest

 PASS  spec/routes/deleteItem.spec.js
 PASS  spec/routes/getItems.spec.js
 PASS  spec/routes/addItem.spec.js
 PASS  spec/routes/updateItem.spec.js
 PASS  spec/persistence/sqlite.spec.js
  ● Console

    console.log
      Using sqlite database at /tmp/todo.db

      at Database.log (src/persistence/sqlite.js:18:25)

    console.log
      Using sqlite database at /tmp/todo.db

      at Database.log (src/persistence/sqlite.js:18:25)

    console.log
      Using sqlite database at /tmp/todo.db

      at Database.log (src/persistence/sqlite.js:18:25)

    console.log
      Using sqlite database at /tmp/todo.db

      at Database.log (src/persistence/sqlite.js:18:25)

    console.log
      Using sqlite database at /tmp/todo.db

      at Database.log (src/persistence/sqlite.js:18:25)


Test Suites: 5 passed, 5 total
Tests:       9 passed, 9 total
Snapshots:   0 total
Time:        2.008 s
Ran all test suites.
```

## Run tests when building

To run your tests when building, you need to update your Dockerfile to add a new test stage.

The following is the updated Dockerfile.

```dockerfile {hl_lines="27-35"}
# syntax=docker/dockerfile:1

ARG NODE_VERSION=18.0.0

FROM node:${NODE_VERSION}-alpine as base
WORKDIR /usr/src/app
EXPOSE 3000

FROM base as dev
RUN --mount=type=bind,source=package.json,target=package.json \

Title: Running Node.js Tests in a Container
Summary
This section focuses on running Node.js tests within a Docker container, both during local development and during the build process. It starts by emphasizing the importance of testing in software development and then demonstrates how to use Docker Compose to execute tests locally. It also provides an updated Dockerfile that includes a new test stage to run tests during the build process.