tmsnvd / nodejs-postgresql-testcontainers

Node.js + Typescript + PostgreSQL + Testcontainers integration testing example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nodejs-postgresql-testcontainers

An example project showcasing how to use @trendyol/jest-testcontainers to write integration tests for your PostgreSQL queries.

Read the article describing this project in detail on Medium.

Project Structure

.
├── migrations        # database migratioons
├── src               # source code of your application
└── test
    ├── integration   # integration tests that contain IO operations(e.g. DB, Queue)
    └── unit          # unit tests for pure business logic / interaction witout IO

Overview

implementation overview

  1. test/integration/ includes various files like jest.config.js, jest-testcontainers-config.js and preset.js for configuring jest-testcontainers to start a PostgreSQL instance.
  2. jest-testcontainers is used to start a PostgreSQL instance and wait for t to start.
  3. migrations/ folder stores database schema migrations that will be applied to the started PostgreSQL instance.
  4. postgresql.environment.ts uses node-pg-migrate to migrate PostgreSQL instance started by the jest-testcontainers.
  5. test/integration/ folder is scanned for files with *.test.ts prefix and tests are ran.

Watch mode is supported so you can start your integration tests in watch mode and refactor your code whilst your PostgreSQL Docker instance is up.

About

Node.js + Typescript + PostgreSQL + Testcontainers integration testing example


Languages

Language:TypeScript 86.3%Language:JavaScript 13.7%