@fmtk/dbtest
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @fmtk/dbtest

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

6.59 kB

Total Files

6

Last publish

Collaborators

  • gordonmleigh