Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@fmtk/dbtest

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fmtk/dbtest

util for running tests in a database

latest
Source
npmnpm
Version
0.2.0
Version published
Maintainers
2
Created
Source

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

FAQs

Package last updated on 06 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts