npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-test-clean-db

v1.1.7

Published

Clean DB, with the migration files pre-applied, per Jest test. No boilerplate code!

Downloads

550

Readme

jest-test-clean-db

npm version Downloads GitHub issues

Purpose

For each jest run, this library will:

  1. Creates a new empty "template" DB.
  2. Applies the TypeORM migrations files on it.
  3. For each pre-selected test:
    1. Make a copy of the "template" DB (which contains the migration files' changes).
    2. Run the test as usual on a empty prepared DB.
  4. Clean up.

Installing

For the latest stable version:

npm install -D jest-test-clean-db@latest

Setup (after installation)

Step 1: Use the test_withCleanDB and describe_withCleanDB functions:

Import the test_withCleanDB and describe_withCleanDB functions, and use them instead of test and describe wherever you need a clean initialized DB in your test files:

// some_test_file.test.ts
import { test_withCleanDB } from 'jest-test-clean-db';

describe('D1', () => {
  test_withCleanDB('Test with a clean DB', ({ dbNameForThisTest, dbDataSource }) => {
    expect(dbNameForThisTest).toBeTrue();

    const userRepository = dbDataSource.getRepository(Users);
    await userRepository.insert({ name: 'test1' });
    const user = await userRepository.findOne({ where: { id: 1 } });
    expect(user).not.toBeNull();
  });

  test('test2', async () => {
    // No clean DB for you
  });
});

describe_withCleanDB('D2', ({ dbNameForThisTest, dbDataSource }) => {
  test('T1', () => {
    // Uses the same clean DB as the 'T2' test
    expect(typeof dbNameForThisTest).toEqual('string');
    dbDataSource.getRepository(Users);
  });

  test('T2', () => {
    // Uses the same clean DB as the 'T1' test
    expect(typeof dbNameForThisTest).toEqual('string');
    dbDataSource.getRepository(Users);
  });
});

Step 2: Env params:

Add the following environment params to reach your testing DB (if this solution doesn't fit your need, leave an issue and let me know):

TEST_POSTGRESQL_HOSTNAME=
TEST_POSTGRESQL_DB_NAME=
TEST_POSTGRESQL_USERNAME=
TEST_POSTGRESQL_PASSWORD=

Step 3: Edit the jest.config.js file:

Add the following values to the following Jest's config file's keys:

// jest.config.js
{
  // ...
  "globalSetup": "jest-test-clean-db/globalSetup",
  "globalTeardown": "jest-test-clean-db/globalTeardown"
  // ...
}

If you just use the functions test_withCleanDB and describe_withCleanDB - everything will seem to be working, however, 2 databases will be created per such test/describe (one will be used as a "template" and copy of it for the test - both will be created per test/describe created with the special functions), and also only one of them (the test database) will be deleted, while the "template" database will remain, which will cause the DB to be filled-up eventually and throw errors. In order to avoid such behavior, and to cause everything to work as expected - you need to add the following values to the following Jest's config file's keys:

Wanna help out?

  • [ ] Add support for other ways to connect to the DB (connection string? import details from a file?).
  • [ ] Add support for other ORMs.
  • [ ] Add support for MySQL and other DBs.
  • [ ] Better test describe_withCleanDB.
  • [ ] Make the test file of the repository's demo project work despite testing failed tests (maybe forward the tests' output to a file, and compare the file with a pre-defined one).