Skip to content

Commit 154a598

Browse files
authored
Tweak vitest/no-standalone-expect rule (#4010)
* Tweak `vitest/no-standalone-expect` rule * update settings.json
1 parent facea0b commit 154a598

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"editor.defaultFormatter": "oxc.oxc-vscode",
99
"editor.formatOnSave": true,
1010
"editor.formatOnSaveMode": "file",
11-
"typescript.enablePromptUseWorkspaceTsdk": true,
12-
"typescript.tsdk": "node_modules/typescript/lib",
11+
"js/ts.tsdk.promptToUseWorkspaceVersion": true,
12+
"js/ts.tsdk.path": "node_modules/typescript/lib",
1313
"files.readonlyInclude": {
1414
"**/routeTree.gen.ts": true
1515
},

eslint.config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,19 @@ copy(
960960
'vitest/no-mocks-import': 1,
961961
'vitest/no-restricted-matchers': 0,
962962
'vitest/no-restricted-vi-methods': 0,
963-
'vitest/no-standalone-expect': 1,
963+
'vitest/no-standalone-expect': [
964+
1,
965+
{
966+
additionalTestBlockFunctions: [
967+
'beforeAll',
968+
'beforeEach',
969+
'afterAll',
970+
'afterEach',
971+
'aroundAll',
972+
'aroundEach'
973+
]
974+
}
975+
],
964976
'vitest/no-test-prefixes': 1,
965977
'vitest/no-test-return-statement': 1,
966978
'vitest/no-unneeded-async-expect-function': 1,

test/failOnConsole.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
let consoleErrorOrConsoleWarnWereCalled = false;
22

33
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;
2110
}
11+
12+
consoleErrorOrConsoleWarnWereCalled = true;
13+
console.log(...params);
14+
};
15+
16+
console.warn = (...params) => {
17+
consoleErrorOrConsoleWarnWereCalled = true;
18+
console.log(...params);
2219
};
2320
});
2421

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
2724
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+
}
3534

3635
consoleErrorOrConsoleWarnWereCalled = false;
3736
});

test/setupBrowser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ beforeEach(async () => {
9090
afterEach(() => {
9191
vi.useRealTimers();
9292

93-
// eslint-disable-next-line vitest/no-standalone-expect
9493
expect
9594
.soft(
9695
document.hasFocus(),

0 commit comments

Comments
 (0)