-
-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathJestCustomEnv.js
More file actions
48 lines (42 loc) · 2.02 KB
/
JestCustomEnv.js
File metadata and controls
48 lines (42 loc) · 2.02 KB
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
// eslint-disable-next-line import/no-extraneous-dependencies
const NodeEnvironment = require('jest-environment-node').default;
class CustomEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
process.on('warning', warning => {
if (warning.name === 'MaxListenersExceededWarning') {
throw new Error(
'MaxListenersExceededWarning detected. If you need more, use events.setMaxListeners(desiredNumber)',
);
}
// TimeoutNegativeWarning / TimeoutOverflowWarning fire from setTimeout when ms is
// negative or > 2^31-1. Almost always a date-arithmetic bug (e.g. `deadline - now`
// where deadline is in the past). Near-zero false-positive surface.
if (
warning.name === 'TimeoutNegativeWarning' ||
warning.name === 'TimeoutOverflowWarning'
) {
throw warning;
}
});
// console.error trap: a real console.error during a test is almost always either an
// expected error path that the test forgot to assert on, or a missing mock that hides
// a real production issue. Tests that legitimately want to silence the error must
// jest.spyOn(console, 'error').mockImplementation(...) — the spy replaces this wrap.
const originalError = this.global.console.error;
this.global.console.error = (...args) => {
originalError.apply(this.global.console, args);
throw new Error(
`Unexpected console.error during test. If this is expected, mock it with ` +
`jest.spyOn(console, 'error').mockImplementation(() => {}). Args: ${args
.map(a => (a instanceof Error ? a.message : String(a)))
.join(' ')}`,
);
};
}
async teardown() {
process.removeAllListeners('warning');
await super.teardown();
}
}
module.exports = CustomEnvironment;