🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

jest-mock-knex

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-knex

## Install

latest
Source
npmnpm
Version
1.18.0
Version published
Weekly downloads
687
16.05%
Maintainers
1
Weekly downloads
 
Created
Source

jest-mock-knex

Install

  npm install jest-mock-knex

How to use

setup jest configs: /package.json

{
  ...
  "jest": {
    "snapshotSerializers": [
      "<rootDir>/node_modules/jest-mock-knex/serializer"
    ]
  }
}

create mock file: /__mocks__/knex.js

import knex from 'jest-mock-knex';

export { client } from 'jest-mock-knex';

export default knex;

process.setMaxListeners(0);

create testing file: */__tests__/*.test.js

import knex, { client } from 'knex';
const pg = knex({
  client: 'pg',
  connection: {
    host: '127.0.0.1',
    user: 'postgres',
    password: null,
    database: 'mock',
  },
});

it(`successfully insert`, () => {
  client.mockReturnValueOnce([1]);
  await pg(tableName).insert({ name });

  expect(client).toHaveBeenLastCalledWith(expect.objectContaining({
    method: 'insert', table: tableName, naem, created_at: 'DATE',
  }));
  expect(client).toHaveBeenCalledTimes(1);
  expect(client).toMatchSnapshot();
});

it(`when mock failed`, () => {
  client.mockReturnValueOnce(new Error('sql error'));
  await expect(pg(tableName).insert({ name })).rejects.toMatchSnapshot();
});

Mock flow

client.mockReturnValueOnce(results);

if (typeof results === 'array') return results;
else if (results instanceof Error) return Promise.reject / Error;
else return (call knex native client)

FAQs

Package last updated on 19 Jan 2018

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