This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

knest

1.1.5 • Public • Published

Knest

NPM Downloads Vulnerabilities Standard Code Style License

Enable rollback for your tests that use knex.

Knest works with most test frameworks out there.

Installation

npm install knest --save-dev

Usage

Example using mocha as the testing framework

This example is part of the knex mocha tests. They can be run using the following npm script.

const assert = require('assert')
const {
  mysql,
  users,
  resetDatabase,
  findUsers,
  createUsers,
  createUser
} = require('./index.spec')
const knest = require('./index').bind(null, mysql)

describe('Mocha & Knest', () => {
  it('should reset the database user table', () => resetDatabase(mysql))

  it('should create user in user table', () =>
    knest(trx =>
      createUser(trx, users[0]).then(record => {
        assert.strictEqual(record.name, users[0].name)
        assert.strictEqual(record.email, users[0].email)
      })
    ))

  it('should create users using multiple transactions', () =>
    knest(trx => createUsers(trx, users)))

  it('should have rolled back all the insert queries', () =>
    findUsers(mysql).then(result => assert.deepStrictEqual(result, [])))

  after(() => process.exit())
})

Reference

Knest exports a single function.

knest(knexConnection, testFn)

What Knest basically does is wrap the test function and adds a knex transaction as the first argument of the test callback function.

returns a promise which will rollback the transaction when it resolves. This is required. Throws if a promise is not returned.

knexConnection

This value should be a knex-connection or a knex transaction object.

testFn(knexTransaction)

The testFn is called when the tests are run. It is called with the following arguments

knexTransaction is an instance of a knex transaction that uses the previously configured knex connection.

Tests

Knest is tested using MySQL. Make sure you have MySQL installed. The ./setup.sql file makes it easy to setup a database and user for running the tests.

sudo mysql -p < setup.sql

Then install, prune and run the tests.

{ npm i && npm prune; } > /dev/null
npm t

> knest@1.1.3 test
> npx standard && npm run test-tape && npm run test-mocha


> knest@1.1.3 test-tape
> tape index.tape.spec.js

TAP version 13
# should reset the database user table
# should create user in user table
ok 1 should be loosely deeply equivalent
# should create users using multiple transactions
# should have rolled back all the insert queries
ok 2 should be loosely deeply equivalent

1..2
# tests 2
# pass  2

# ok


> knest@1.1.3 test-mocha
> mocha index.mocha.spec.js



  Mocha & Knest
    ✔ should reset the database user table (115ms)
    ✔ should create user in user table
    ✔ should create users using multiple transactions
    ✔ should have rolled back all the insert queries

Contribute

Feel free to contribute in whatever way you deem to be valuable.

Read more in the CONTRIBUTING.md

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i knest

Weekly Downloads

1

Version

1.1.5

License

MIT

Unpacked Size

15.2 kB

Total Files

21

Last publish

Collaborators

  • bas080