Skip to content

futurematik/dbtest

Repository files navigation

@fmtk/dbtest

This package creates a test database for jest tests.

Use it like this:

import { connectTestDatabase } from '@fmtk/dbtest';

describe('my cool feature', () => {
  // don't destructure here
  const context = connectTestDatabase();

  it('does something', async () => {
    const result = context.pool.query('SELECT current_database()');
  });
});

API

Options

interface Options {
  connection?: pg.ClientConfig; // from node-postgres
  eachTest?: boolean; // create a new database for each test
  initialDatabase?: string; // initial db to connect to (default 'postgres')
}

connectTestDatabase

function connectTestDatabase(options?: Options): { pool: pg.Pool };

This will register Jest beforeAll and afterAll handlers (or beforeEach and afterEach if eachTest is true) which create and delete a test database. The database is named test_xxxxxx where xxxxxx is a random string generated by nanoid.

The connection parameters can be passed in the first argument. If database is specified, this will be used instead of the auto-generated name.

If no user is specified, it will default to postgres.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published