forked from beyondessential/tupaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.js
68 lines (59 loc) · 1.79 KB
/
jest.setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Tupaia
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd
*/
/* eslint-env jest */
require('jest-extended');
require('jest-expect-message');
const winston = require('winston');
const { addCustomJestMatchers } = require('@tupaia/utils');
const customMatchers = [
{
description: {
name: 'toHaveBeenCalledOnceWith',
receives: 'jest.fn()',
},
matcher: (expectChain, received, expected) => {
expectChain(received).toHaveBeenCalledTimes(1);
expectChain(received).toHaveBeenCalledWith(...expected);
},
negationDiff: (extendApi, _, expected) =>
`Expected spy not to have been called ${extendApi.utils.RECEIVED_COLOR(
'once',
)} with ${extendApi.utils.printReceived(expected)}`,
},
{
description: {
name: 'toBeRejectedWith',
},
matcher: async (expectChain, received, [expected]) => {
expectChain.hasAssertions();
let isErrorThrown = false;
return received
.catch(error => {
isErrorThrown = true;
// Throw the error again in a sync function so that
// the standard `toThrow` matcher from `jest` can be used
expect(() => {
throw error;
}).toThrow(expected);
})
.then(() => {
if (!isErrorThrown) {
// Force an error matching failure using the standard `jest` error matchers
expect(() => {}).toThrow(expected);
}
});
},
negationDiff: (extendApi, _, expected) =>
`Expected promise not to have been rejected with ${extendApi.utils.RECEIVED_COLOR(
expected,
)} `,
isAsync: true,
},
];
addCustomJestMatchers(expect, customMatchers);
// Silence winston logs
winston.configure({
transports: [new winston.transports.Console({ silent: true })],
});