1.18.0 • Published 6 years ago
jest-mock-knex v1.18.0
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)
1.18.0
6 years ago
1.17.7
6 years ago
1.17.6
6 years ago
1.7.5
7 years ago
1.7.4
7 years ago
1.7.3
7 years ago
1.7.2
7 years ago
1.7.1
7 years ago
1.7.0
7 years ago
1.6.4
7 years ago
1.6.3
7 years ago
1.6.2
7 years ago
1.6.1
7 years ago
1.6.0
7 years ago
1.5.0
7 years ago
1.4.2
7 years ago
1.4.1
7 years ago
1.4.0
7 years ago
1.3.2
7 years ago
1.3.1
7 years ago
1.3.0
7 years ago
1.2.8
7 years ago
1.2.7
7 years ago
1.2.6
7 years ago
1.2.5
7 years ago
1.2.4
7 years ago
1.2.2
7 years ago
1.2.1
7 years ago
1.2.0
7 years ago
1.1.0
7 years ago
1.0.1
7 years ago
1.0.0
7 years ago