Skip to content

Commit d1df105

Browse files
tests: enable printConsoleTrace, add failOnConsole.ts (#3956)
* tests: enable printConsoleTrace, add failOnConsole.ts * fix biome config --------- Co-authored-by: Aman Mahajan <amahajan@stratag.com>
1 parent 85c01f1 commit d1df105

4 files changed

Lines changed: 65 additions & 5 deletions

File tree

biome.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@
360360
}
361361
}
362362
},
363+
{
364+
"includes": ["test/failOnConsole.ts"],
365+
"linter": {
366+
"rules": {
367+
"suspicious": {
368+
"noConsole": "off"
369+
}
370+
}
371+
}
372+
},
363373
{
364374
"includes": ["**/*.js", "**/rolldown.config.ts"],
365375
"linter": {

eslint.config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ export default defineConfig([
630630
'vitest/no-disabled-tests': 0,
631631
'vitest/no-done-callback': 1,
632632
'vitest/no-duplicate-hooks': 1,
633-
'vitest/no-focused-tests': 1,
634-
'vitest/no-hooks': 1,
633+
'vitest/no-focused-tests': [1, { fixable: false }],
634+
'vitest/no-hooks': 0,
635635
'vitest/no-identical-title': 1,
636636
'vitest/no-import-node-test': 1,
637637
'vitest/no-importing-vitest-globals': 1,
@@ -741,6 +741,16 @@ export default defineConfig([
741741
}
742742
},
743743

744+
{
745+
name: 'tools',
746+
747+
files: ['test/failOnConsole.ts'],
748+
749+
rules: {
750+
'no-console': 0
751+
}
752+
},
753+
744754
{
745755
name: 'node',
746756

test/failOnConsole.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
let consoleErrorOrConsoleWarnWereCalled = false;
2+
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);
21+
}
22+
};
23+
});
24+
25+
afterEach(() => {
26+
if (!consoleErrorOrConsoleWarnWereCalled) {
27+
return;
28+
}
29+
30+
consoleErrorOrConsoleWarnWereCalled = false;
31+
32+
// Errors thrown in `afterEach` will short-circuit subsequent `afterEach` hooks,
33+
// thus preventing tests from being cleaned up properly and affecting other tests.
34+
// We must therefore wait for tests to "finish" before throwing the error.
35+
onTestFinished(() => {
36+
throw new Error('console.error() and/or console.warn() were called during the test');
37+
});
38+
});

vite.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default defineConfig(
124124
test: {
125125
dir: 'test',
126126
globals: true,
127+
printConsoleTrace: true,
127128
coverage: {
128129
provider: 'istanbul',
129130
enabled: isCI,
@@ -156,7 +157,7 @@ export default defineConfig(
156157
headless: true,
157158
screenshotFailures: !isCI
158159
},
159-
setupFiles: ['test/setupBrowser.ts']
160+
setupFiles: ['test/setupBrowser.ts', 'test/failOnConsole.ts']
160161
}
161162
},
162163
{
@@ -171,15 +172,16 @@ export default defineConfig(
171172
headless: true,
172173
screenshotFailures: false
173174
},
174-
setupFiles: ['test/setupBrowser.ts']
175+
setupFiles: ['test/setupBrowser.ts', 'test/failOnConsole.ts']
175176
}
176177
},
177178
{
178179
extends: true,
179180
test: {
180181
name: 'node',
181182
include: ['node/**/*.test.*'],
182-
environment: 'node'
183+
environment: 'node',
184+
setupFiles: ['test/failOnConsole.ts']
183185
}
184186
}
185187
]

0 commit comments

Comments
 (0)