Appearance
Fresh
Create the project
Back
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
Create the Node.js project
Copy as Markdown
Open MarkdownAsk Docs AIClaudeOpen in Claude
Table of contents
Initialize the project
Create a new Node.js project:
console
$ npm init -yAdd pg, jest, and @testcontainers/postgresql as dependencies:
console
$ npm install pg --save
$ npm install jest @testcontainers/postgresql --save-devImplement the customer repository
Create src/customer-repository.js with functions to manage customers in PostgreSQL:
javascript
async function createCustomerTable(client) {
const sql =
"CREATE TABLE IF NOT EXISTS customers (id INT NOT NULL, name VARCHAR NOT NULL, PRIMARY KEY (id))";
await client.query(sql);
}
async function createCustomer(client, customer) {
const sql = "INSERT INTO customers (id, name) VALUES($1, $2)";
await client.query(sql, [customer.id, customer.name]);
}
async function getCustomers(client) {
const sql = "SELECT * FROM customers";
const result = await client.query(sql);
return result.rows;
}
module.exports = { createCustomerTable, createCustomer, getCustomers };The module provides three functions:
createCustomerTable()creates thecustomerstable if it doesn't exist.createCustomer()inserts a customer record.getCustomers()fetches all customer records.