|
1 | 1 | let consoleErrorOrConsoleWarnWereCalled = false; |
2 | 2 |
|
3 | 3 | beforeAll(() => { |
4 | | - // replace instead of mutating `console` to avoid infinite loops |
5 | | - globalThis.console = { |
6 | | - ...console, |
7 | | - error(...params) { |
8 | | - if ( |
9 | | - params[0] instanceof Error && |
10 | | - params[0].message === 'ResizeObserver loop completed with undelivered notifications.' |
11 | | - ) { |
12 | | - return; |
13 | | - } |
14 | | - |
15 | | - consoleErrorOrConsoleWarnWereCalled = true; |
16 | | - console.log(...params); |
17 | | - }, |
18 | | - warn(...params) { |
19 | | - consoleErrorOrConsoleWarnWereCalled = true; |
20 | | - console.log(...params); |
| 4 | + console.error = (...params) => { |
| 5 | + if ( |
| 6 | + params[0] instanceof Error && |
| 7 | + params[0].message === 'ResizeObserver loop completed with undelivered notifications.' |
| 8 | + ) { |
| 9 | + return; |
21 | 10 | } |
| 11 | + |
| 12 | + consoleErrorOrConsoleWarnWereCalled = true; |
| 13 | + console.log(...params); |
| 14 | + }; |
| 15 | + |
| 16 | + console.warn = (...params) => { |
| 17 | + consoleErrorOrConsoleWarnWereCalled = true; |
| 18 | + console.log(...params); |
22 | 19 | }; |
23 | 20 | }); |
24 | 21 |
|
25 | | -afterEach(() => { |
26 | | - // Wait for both the test and `afterEach` hooks to complete to ensure all logs are caught |
| 22 | +afterEach(({ task, signal }) => { |
| 23 | + // Wait for the test and all `afterEach` hooks to complete to ensure all logs are caught |
27 | 24 | onTestFinished(() => { |
28 | | - // eslint-disable-next-line vitest/no-standalone-expect |
29 | | - expect |
30 | | - .soft( |
31 | | - consoleErrorOrConsoleWarnWereCalled, |
32 | | - 'console.error() and/or console.warn() were called during the test' |
33 | | - ) |
34 | | - .toBe(false); |
| 25 | + // avoid failing test runs twice |
| 26 | + if (task.result!.state !== 'fail' || signal.aborted) { |
| 27 | + expect |
| 28 | + .soft( |
| 29 | + consoleErrorOrConsoleWarnWereCalled, |
| 30 | + 'errors/warnings were logged to the console during the test' |
| 31 | + ) |
| 32 | + .toBe(false); |
| 33 | + } |
35 | 34 |
|
36 | 35 | consoleErrorOrConsoleWarnWereCalled = false; |
37 | 36 | }); |
|
0 commit comments