Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dbfixtures

PedroHenriques993MIT2.1.0TypeScript support: included

A NodeJS equivalent to DbUnit.

db fixtures, test fixtures, fixtures

readme

Build Status

Fixtures Manager

An abstraction layer for handling database fixtures for automated testing purposes, providing a standardized interface across different database systems.

Installation

npm install --save-dev dbfixtures

Features

  • Test runner agnostic
  • No dependencies
  • Written in TypeScript
  • Standardized interface across multiple database systems
  • Easily set your database for each test's needs

NodeJS versions

  • Package version >=2.1.* supports NodeJS v10 or higher.

  • Package version >=2.0.* supports NodeJS v8 or higher.

  • Package version >=1.1.* supports NodeJS v8 or higher.
    NOTE: For versions 8 and 9 the node flag --harmony_promise_finally is required

  • Package version 1.0.* supports NodeJS v7 or higher.

Drivers

This package will use drivers to handle the database operations. Each driver will be dedicated to 1 databse system (ex: MySQL, MongoDb).
You can set as many drivers as needed and the fixtures will be sent to each one.

Driver interface

The drivers are expected to use the following interface

// clears the specified "tables" from any content
truncate: (tableNames: string[]) => Promise<void>

// inserts the supplied "rows" into the specified "table"
insertFixtures: (tableName: string, fixtures: {}[]) => Promise<void>

// terminates the connection to the database
close: () => Promise<void>

Current official drivers

Usage

This package exposes 3 functions

  • setDrivers(...newDrivers: IDriver[]): void: call this function with the array of driver instances to be used when fixtures are inserted.

  • insertFixtures(fixtures: IFixtures): Promise<void>: call this function with the fixtures to be sent to each registered driver.
    Note: the fixtures will be inserted in the order they are provided.

  • closeDrivers(): Promise<void>: call this function to run any necessary cleanup operations on all registered drivers (ex: close DB connections).

The IDriver interface is described in the section Driver interface above.

The IFixtures interface is an object with strings as keys and arrays as values.
The keys are "table" names and for each one provide an array of objects, each representing a "row" to be inserted.

Example

This example uses Mocha as the potential test runner.

const dbfixtures = require('dbfixtures');
const fixturesMysqlDriver = require('dbfixtures-mysql-driver');

const mysqlConnectionInfo = {
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: '',
  database: 'fixtures_test',
};
const fixtures = {
  'roles': [
    { id: 1, name: 'role 1' },
    { id: 2, name: 'role 2' },
  ],
  'users': [
    { id: 1, email: 'myemail@test.net', role_id: 2 },
    { id: 2, email: 'test@gmail.com', role_id: 1 },
    { id: 3, email: 'another@email.org', role_id: 1 },
  ],
};

describe('fixtures example', function () {
  before(async function () {
    const mysqlDriver = await fixturesMysqlDriver.create(mysqlConnectionInfo);
    dbfixtures.setDrivers(mysqlDriver);
  });

  after(async function () {
    await dbfixtures.closeDrivers();
  });

  beforeEach(async function () {
    await dbfixtures.insertFixtures(fixtures);
  });

  it('should have the database seeded with the fixtures', function () {
    // ...
  });
});

How It Works

Each registered driver will be called to:

  • clear the "tables" that will be used in the current fixture insertion operation from any content.

  • insert the fixtures in the order they were provided.

  • terminate the connection to their database.

Testing This Package

  • cd into the package's directory
  • run npm install
  • run npm run build

  • for unit tests run npm test -- test/unit/

changelog

Changelog

[2.1.0] - 2020-09-29

Added

  • Link to the DynamoDb driver in the README file
  • New versions of NPM dev dependencies

[2.0.0] - 2019-09-12

Added

  • The closeDrivers exported function, which will call each registered driver's close function

Changed

  • insertFixtures() no longer calls each registered driver's close(), removing the need to register the drivers before each test
  • Updated NPM devDependencies to latest versions

Removed

  • Deprecated linter option

[1.1.1] - 2019-01-10

Added

  • Information about supported NodeJS versions to the README.md file

Fixed

  • Incorrect minimal supported NodeJS version, which is 8 and not 10, provided the --harmony_promise_finally flag is used when running node

[1.1.0] - 2019-01-03

Changed

  • Refactored the insertFixtures() function to improve performance when handling multiple drivers at the same time
  • Improved unit tests, to have more coverage and have only 1 assert per test function
  • Cleaned up the NPM scripts
  • Updated all NPM devDependencies to latests versions
  • Improved the README.md file with mentions to the now available MongoDB driver and other overall text improvements
  • Updated travis ci configuration file to include only versions of NodeJS >= 10, due to the use of Promise's finally() method

Removed

  • NPM not needed devDependencies: @types/node and ts-node

[1.0.1] - 2018-02-09

Fixed

  • Type definitions for IFixtures and IDriver, replacing [{}] with {}[]
  • Added missing package-lock.json file

[1.0.0] - 2018-01-20

Added

  • First version of the code