How to run Node.js tests in a container
Requirements How to containerize a node.js application How to use containers for Node.js development Introduction Testing is a critical component of modern software development practices, though its implementation varies across development teams. Common testing methodologies include unit tests, integration tests, and end-to-end testing. In this guide, we'll focus on executing unit tests within Docker - both during development and as part of the build process. Run tests when developing locally The sample application includes the Jest testing framework and comes with preconfigured tests in the spec directory. During local development, you can execute these tests using Docker Compose. Run the following command to execute the test script (defined in package.json) within a container: docker compose run server npm run test You should see output like the following. Run tests when building To integrate testing into your build process, you'll need to add a dedicated test stage to your multi-stage Dockerfile. This ensures tests are automatically executed during the image build phase. The following is the updated Dockerfile. For the test stage in your Dockerfile, use RUN rather than CMD to execute tests. This is because: RUN executes during the image build process and will cause the build to fail if tests fail CMD executes when the container starts, which is too late for build-time validation To build with test execution, run: docker build -t node-docker-image-test --progress=plain --no-cache --target test . Thanks for staying till the end

Requirements
Introduction
Testing is a critical component of modern software development practices, though its implementation varies across development teams. Common testing methodologies include unit tests, integration tests, and end-to-end testing. In this guide, we'll focus on executing unit tests within Docker - both during development and as part of the build process.
Run tests when developing locally
The sample application includes the Jest testing framework and comes with preconfigured tests in the spec directory. During local development, you can execute these tests using Docker Compose.
Run the following command to execute the test script (defined in package.json) within a container:
docker compose run server npm run test
You should see output like the following.
Run tests when building
To integrate testing into your build process, you'll need to add a dedicated test stage to your multi-stage Dockerfile. This ensures tests are automatically executed during the image build phase.
The following is the updated Dockerfile.
For the test stage in your Dockerfile, use RUN rather than CMD to execute tests. This is because:
RUN executes during the image build process and will cause the build to fail if tests fail
CMD executes when the container starts, which is too late for build-time validation
To build with test execution, run:
docker build -t node-docker-image-test --progress=plain --no-cache --target test .
Thanks for staying till the end