Skip to content

Commit 5393fa9

Browse files
authored
Merge pull request storybookjs#34697 from storybookjs/jeppe/improve-fail-on-error
Test: Improve stack trace when failing on console.warn/error
2 parents 3999e3c + 7b7e8d5 commit 5393fa9

7 files changed

Lines changed: 272 additions & 287 deletions

File tree

code/addons/vitest/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
"@types/micromatch": "^4.0.0",
8686
"@types/node": "^22.19.1",
8787
"@types/semver": "^7.7.1",
88-
"@vitest/browser-playwright": "^4.1.0",
89-
"@vitest/runner": "^4.1.0",
88+
"@vitest/browser-playwright": "^4.1.5",
89+
"@vitest/runner": "^4.1.5",
9090
"empathic": "^2.0.0",
9191
"es-toolkit": "^1.43.0",
9292
"istanbul-lib-report": "^3.0.1",
@@ -102,7 +102,7 @@
102102
"tree-kill": "^1.2.2",
103103
"ts-dedent": "^2.2.0",
104104
"typescript": "^5.9.3",
105-
"vitest": "^4.1.0"
105+
"vitest": "^4.1.5"
106106
},
107107
"peerDependencies": {
108108
"@vitest/browser": "^3.0.0 || ^4.0.0",

code/addons/vitest/src/node/coverage-reporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { TestManager } from './test-manager.ts';
88

99
export type StorybookCoverageReporterOptions = {
1010
testManager: TestManager;
11-
coverageOptions: ResolvedCoverageOptions<'v8'> | undefined;
11+
coverageOptions: ResolvedCoverageOptions | undefined;
1212
};
1313

1414
class StorybookCoverageReporter extends ReportBase implements Partial<Visitor> {

code/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@
9595
"@types/react-dom": "^18.0.11",
9696
"@vitejs/plugin-react": "^4.3.2",
9797
"@vitejs/plugin-vue": "^4.4.0",
98-
"@vitest/browser": "^4.1.0",
99-
"@vitest/browser-playwright": "^4.1.0",
100-
"@vitest/coverage-istanbul": "^4.1.0",
101-
"@vitest/coverage-v8": "^4.1.0",
98+
"@vitest/browser": "^4.1.5",
99+
"@vitest/browser-playwright": "^4.1.5",
100+
"@vitest/coverage-istanbul": "^4.1.5",
101+
"@vitest/coverage-v8": "^4.1.5",
102102
"chromatic": "^13.3.4",
103103
"create-storybook": "workspace:*",
104104
"cross-env": "^7.0.3",
@@ -125,7 +125,7 @@
125125
"uuid": "^11.1.0",
126126
"vite": "^7.0.4",
127127
"vite-plugin-inspect": "^11.0.0",
128-
"vitest": "^4.1.0",
128+
"vitest": "^4.1.5",
129129
"wait-on": "^8.0.3"
130130
},
131131
"devDependencies": {

code/vitest-setup.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,49 @@ import { dedent } from 'ts-dedent';
66
import './core/src/shared/utils/toHaveLiveRegion.ts';
77
import { toHaveLiveRegion } from './core/src/shared/utils/toHaveLiveRegion.ts';
88

9-
const ignoreList = [
10-
(error: any) => error.message.includes('":nth-child" is potentially unsafe'),
11-
(error: any) => error.message.includes('":first-child" is potentially unsafe'),
12-
(error: any) =>
13-
error.message.match(
9+
const ALLOWED_CONSOLE_PREDICATES: ((...args: any[]) => boolean)[] = [
10+
(message) => message.includes('":nth-child" is potentially unsafe'),
11+
(message) => message.includes('":first-child" is potentially unsafe'),
12+
(message) =>
13+
message.includes(
1414
`Support for defaultProps will be removed from function components in a future major release`
1515
),
16-
(error: any) => error.message.match(/Browserslist: .*Please run:/),
17-
(error: any) => error.message.includes('Consider adding an error boundary'),
18-
(error: any) =>
19-
error.message.includes('react-async-component-lifecycle-hooks') &&
20-
error.stack.includes('addons/knobs/src/components/__tests__/Options.js'),
16+
(message) => /^Browserslist: .* Please run:/.test(message),
17+
(message) => message.includes('Consider adding an error boundary'),
2118
// React will log this error even if you catch an error with a boundary. I guess it's to
2219
// help in development. See https://github.com/facebook/react/issues/15069
23-
(error: any) =>
24-
error.message.match(
25-
/React will try to recreate this component tree from scratch using the error boundary you provided/
20+
(message) =>
21+
message.includes(
22+
'React will try to recreate this component tree from scratch using the error boundary you provided'
2623
),
27-
(error: any) => error.message.includes('Lit is in dev mode. Not recommended for production!'),
28-
(error: any) => error.message.includes('error: `DialogContent` requires a `DialogTitle`'),
29-
(error: any) =>
30-
error.message.includes(
24+
(message) => message.includes('Lit is in dev mode. Not recommended for production!'),
25+
(message) => message.includes('error: `DialogContent` requires a `DialogTitle`'),
26+
(message) =>
27+
message.includes(
3128
"importMetaResolve from within Storybook is being used in a Vitest test, but it shouldn't be. Please report this at https://github.com/storybookjs/storybook/issues/new?template=bug_report.yml"
3229
),
33-
(error: any) =>
34-
error.message.includes('<Pressable> child must forward its ref to a DOM element.'),
35-
(error: any) =>
36-
error.message.includes('<Focusable> child must forward its ref to a DOM element.'),
37-
(error: any) => error.message.includes('Please ensure the tabIndex prop is passed through.'),
30+
(message) => message.includes('<Pressable> child must forward its ref to a DOM element.'),
31+
(message) => message.includes('<Focusable> child must forward its ref to a DOM element.'),
32+
(message) => message.includes('Please ensure the tabIndex prop is passed through.'),
3833
// Vitest only warns about this if the import comes from a file outside of `node_modules`.
3934
// This only occurs locally for us and is safe to ignore.
4035
// It will stop once we start importing from `vitest/browser` instead (not a Vitest 3 compatible change).
4136
// TODO: can be removed in SB11 (when/if we remove Vitest 3 support)
42-
(error: any) =>
43-
error.message.includes('tries to load a deprecated "@vitest/browser/context" module.'),
37+
(message) => message.includes('tries to load a deprecated "@vitest/browser/context" module.'),
4438
];
4539

46-
const throwMessage = (type: any, message: any) => {
47-
// eslint-disable-next-line local-rules/no-uncategorized-errors
48-
const error = new Error(`${type}${message}`);
49-
if (!ignoreList.reduce((acc, item) => acc || item(error), false)) {
50-
throw error;
51-
}
52-
};
53-
const throwWarning = (message: any) => throwMessage('warn: ', message);
54-
const throwError = (message: any) => throwMessage('error: ', message);
40+
(['warn', 'error'] as const).forEach((type) => {
41+
const failOnConsole = vi.defineHelper((...args) => {
42+
if (ALLOWED_CONSOLE_PREDICATES.some((predicate) => predicate(...args))) {
43+
return;
44+
}
45+
expect.fail(`Unexpected console.${type} call with arguments:\n${args.join('\n')}`);
46+
});
47+
vi.spyOn(console, type).mockImplementation(failOnConsole);
48+
});
5549

5650
globalThis.FEATURES ??= {};
5751

58-
vi.spyOn(console, 'warn').mockImplementation(throwWarning);
59-
vi.spyOn(console, 'error').mockImplementation(throwError);
60-
6152
expect.extend({
6253
toMatchPaths(regex: RegExp, paths: string[]) {
6354
const matched = paths.map((p) => !!p.match(regex));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"oxfmt": "^0.41.0",
7575
"std-env": "^4.0.0",
7676
"vite": "^7.0.4",
77-
"vitest": "^4.1.0"
77+
"vitest": "^4.1.5"
7878
},
7979
"packageManager": "yarn@4.10.3"
8080
}

scripts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"@typescript-eslint/eslint-plugin": "^8.48.0",
7979
"@typescript-eslint/experimental-utils": "^5.62.0",
8080
"@typescript-eslint/parser": "^8.48.0",
81-
"@vitest/coverage-v8": "^4.1.0",
81+
"@vitest/coverage-v8": "^4.1.5",
8282
"ansi-regex": "^6.0.1",
8383
"chromatic": "^13.3.4",
8484
"citty": "^0.2.1",
@@ -161,7 +161,7 @@
161161
"type-fest": "~2.19",
162162
"typescript": "^5.9.3",
163163
"uuid": "^9.0.1",
164-
"vitest": "^4.1.0",
164+
"vitest": "^4.1.5",
165165
"wait-on": "^8.0.3",
166166
"window-size": "^1.1.1",
167167
"yaml": "^2.8.1",

0 commit comments

Comments
 (0)