-
Notifications
You must be signed in to change notification settings - Fork 7
/
jest.setup.js
77 lines (66 loc) · 2.09 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
69
70
71
72
73
74
75
76
77
/**
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
/* eslint-env jest */
require('jest-extended');
const dotenv = require('dotenv');
dotenv.config();
/**
* Note: Due to incompatibility with jest-expected-message and jest 27+ we are disabling
* the custom error messages for these tests. Will re-instate once the fix gets merged:
* https://github.com/mattphillips/jest-expect-message/pull/40
*/
// 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 })],
});