Skip to content
Fresh

Run tests

Back

Guides

Getting started with Testcontainers for Node.js

Learn how to create a Node.js application and test database interactions using Testcontainers for Node.js with a real PostgreSQL instance.

JavaScript Testing with Docker

15 minutes

1

Create the project

2

Write tests

3

Run tests

« Back to all guides

Run tests and next steps

Copy as Markdown

Open MarkdownAsk Docs AIClaudeOpen in Claude

Table of contents


Run the tests

Add the test script to package.json if it isn't there already:

json
{
  "scripts": {
    "test": "jest"
  }
}

Then run the tests:

console
$ npm test

You should see output like:

text
 PASS  src/customer-repository.test.js
  Customer Repository
    ✓ should create and return multiple customers (5 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total

To see what Testcontainers is doing under the hood — which containers it starts, what versions it uses — set the DEBUG environment variable:

console
$ DEBUG=testcontainers* npm test

Summary

The Testcontainers for Node.js library helps you write integration tests using the same type of database (Postgres) that you use in production, instead of mocks. Because you aren't using mocks and instead talk to real services, you're free to refactor code and still verify that the application works as expected.

In addition to PostgreSQL, Testcontainers provides dedicated modules for many SQL databases, NoSQL databases, messaging queues, and more.

To learn more about Testcontainers, visit the Testcontainers overview.

Further reading