0.2.0 • Published 5 years ago

@fmtk/dbtest v0.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

@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.

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago