-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathfailOnConsole.ts
More file actions
31 lines (27 loc) · 903 Bytes
/
failOnConsole.ts
File metadata and controls
31 lines (27 loc) · 903 Bytes
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
let consoleErrorOrConsoleWarnWereCalled = false;
beforeAll(() => {
// replace instead of mutating `console` to avoid infinite loops
globalThis.console = {
...console,
error(...params) {
consoleErrorOrConsoleWarnWereCalled = true;
console.log(...params);
},
warn(...params) {
consoleErrorOrConsoleWarnWereCalled = true;
console.log(...params);
}
};
});
afterEach(() => {
if (!consoleErrorOrConsoleWarnWereCalled) {
return;
}
consoleErrorOrConsoleWarnWereCalled = false;
// Errors thrown in `afterEach` will short-circuit subsequent `afterEach` hooks,
// thus preventing tests from being cleaned up properly and affecting other tests.
// We must therefore wait for tests to "finish" before throwing the error.
onTestFinished(() => {
throw new Error('console.error() and/or console.warn() were called during the test');
});
});