# jest-mock-knex

> ## Install

Latest version **1.18.0** (published 2018-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-mock-knex
pnpm add jest-mock-knex
yarn add jest-mock-knex
bun add jest-mock-knex
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.18.0 |
| Published | 2018-01-19 |
| First published | 2017-04-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tmotx |
| Maintainers | yutin1987 |

## Links

- npm: https://www.npmjs.com/package/jest-mock-knex
- Repository: https://github.com/tmotx/jest-mock
- Homepage: https://github.com/tmotx/jest-mock#readme
- Issues: https://github.com/tmotx/jest-mock/issues
- npm.io page: https://npm.io/package/jest-mock-knex

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4

## Recent versions

- 1.18.0 (latest) — 2018-01-19
- 1.17.7 — 2017-11-15
- 1.17.6 — 2017-11-15
- 1.7.5 — 2017-08-28
- 1.7.4 — 2017-08-25
- 1.7.3 — 2017-08-25
- 1.7.2 — 2017-08-25
- 1.7.1 — 2017-08-25
- 1.7.0 — 2017-08-24
- 1.6.4 — 2017-08-10
- 1.6.3 — 2017-07-21
- 1.6.2 — 2017-07-21
- 1.6.1 — 2017-07-07
- 1.6.0 — 2017-07-07
- 1.5.0 — 2017-06-24
- … 17 more at https://npm.io/package/jest-mock-knex/versions

## README

# 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)
```

---
_Source: https://npm.io/package/jest-mock-knex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
