Skip to content

Commit b1b2c6b

Browse files
authored
Merge pull request #28906 from storybookjs/yann/fix-setup-file-message
Vitest Addon: Fix error message logic in set up file
2 parents bf2e894 + 53d295d commit b1b2c6b

File tree

5 files changed

+97
-19
lines changed

5 files changed

+97
-19
lines changed

code/.storybook/vitest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default mergeConfig(
3333
include: [
3434
// TODO: test all core and addon stories later
3535
// './core/**/components/**/*.{story,stories}.?(c|m)[jt]s?(x)',
36-
'../addons/interactions/src/**/*.{story,stories}.?(c|m)[jt]s?(x)',
36+
'../addons/**/src/**/*.{story,stories}.?(c|m)[jt]s?(x)',
3737
],
3838
exclude: [
3939
...defaultExclude,

code/addons/onboarding/src/components/HighlightElement/HighlightElement.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import type { Meta, StoryObj } from '@storybook/react';
4-
import { expect, within } from '@storybook/test';
4+
import { expect, waitFor, within } from '@storybook/test';
55

66
import { HighlightElement } from './HighlightElement';
77

@@ -38,7 +38,7 @@ export const Default: Story = {
3838
play: async ({ canvasElement }) => {
3939
const canvas = within(canvasElement.parentElement!);
4040
const button = canvas.getByRole('button');
41-
await expect(button).toHaveStyle('box-shadow: rgba(2,156,253,1) 0 0 2px 1px');
41+
await waitFor(() => expect(button).toHaveStyle('box-shadow: rgba(2,156,253,1) 0 0 2px 1px'));
4242
},
4343
};
4444

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-disable no-underscore-dangle */
2+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
3+
4+
import { type Task, modifyErrorMessage } from './setup-file';
5+
6+
describe('modifyErrorMessage', () => {
7+
const originalUrl = import.meta.env.__STORYBOOK_URL__;
8+
beforeEach(() => {
9+
import.meta.env.__STORYBOOK_URL__ = 'http://localhost:6006';
10+
});
11+
12+
afterEach(() => {
13+
import.meta.env.__STORYBOOK_URL__ = originalUrl;
14+
});
15+
16+
it('should modify the error message if the test is failing and there is a storyId in the task meta', () => {
17+
const task: Task = {
18+
type: 'test',
19+
result: {
20+
state: 'fail',
21+
errors: [{ message: 'Original error message' }],
22+
},
23+
meta: { storyId: 'my-story' },
24+
};
25+
26+
modifyErrorMessage({ task });
27+
28+
expect(task.result?.errors?.[0].message).toMatchInlineSnapshot(`
29+
"
30+
Click to debug the error directly in Storybook: http://localhost:6006/?path=/story/my-story&addonPanel=storybook/interactions/panel
31+
32+
Original error message"
33+
`);
34+
expect(task.result?.errors?.[0].message).toContain('Original error message');
35+
});
36+
37+
it('should not modify the error message if task type is not "test"', () => {
38+
const task: Task = {
39+
type: 'custom',
40+
result: {
41+
state: 'fail',
42+
errors: [{ message: 'Original error message' }],
43+
},
44+
meta: { storyId: 'my-story' },
45+
};
46+
47+
modifyErrorMessage({ task });
48+
49+
expect(task.result?.errors?.[0].message).toBe('Original error message');
50+
});
51+
52+
it('should not modify the error message if task result state is not "fail"', () => {
53+
const task: Task = {
54+
type: 'test',
55+
result: {
56+
state: 'pass',
57+
},
58+
meta: { storyId: 'my-story' },
59+
};
60+
61+
modifyErrorMessage({ task });
62+
63+
expect(task.result?.errors).toBeUndefined();
64+
});
65+
66+
it('should not modify the error message if meta.storyId is not present', () => {
67+
const task: Task = {
68+
type: 'test',
69+
result: {
70+
state: 'fail',
71+
errors: [{ message: 'Non story test failure' }],
72+
},
73+
meta: {},
74+
};
75+
76+
modifyErrorMessage({ task });
77+
78+
expect(task.result?.errors?.[0].message).toBe('Non story test failure');
79+
});
80+
});
+13-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22

33
/* eslint-disable no-underscore-dangle */
4-
import { afterAll, vi } from 'vitest';
5-
import type { RunnerTask, TaskMeta } from 'vitest';
4+
import { afterEach, vi } from 'vitest';
5+
import type { RunnerTask } from 'vitest';
66

77
import { Channel } from 'storybook/internal/channels';
88

@@ -13,28 +13,26 @@ declare global {
1313
var __STORYBOOK_ADDONS_CHANNEL__: Channel;
1414
}
1515

16-
type ExtendedMeta = TaskMeta & { storyId: string; hasPlayFunction: boolean };
16+
export type Task = Partial<RunnerTask> & {
17+
meta: Record<string, any>;
18+
};
1719

1820
const transport = { setHandler: vi.fn(), send: vi.fn() };
19-
globalThis.__STORYBOOK_ADDONS_CHANNEL__ = new Channel({ transport });
21+
globalThis.__STORYBOOK_ADDONS_CHANNEL__ ??= new Channel({ transport });
2022

21-
// The purpose of this set up file is to modify the error message of failed tests
22-
// and inject a link to the story in Storybook
23-
const modifyErrorMessage = (currentTask: RunnerTask) => {
24-
const meta = currentTask.meta as ExtendedMeta;
23+
export const modifyErrorMessage = ({ task }: { task: Task }) => {
24+
const meta = task.meta;
2525
if (
26-
currentTask.type === 'test' &&
27-
currentTask.result?.state === 'fail' &&
26+
task.type === 'test' &&
27+
task.result?.state === 'fail' &&
2828
meta.storyId &&
29-
currentTask.result.errors?.[0]
29+
task.result.errors?.[0]
3030
) {
31-
const currentError = currentTask.result.errors[0];
31+
const currentError = task.result.errors[0];
3232
const storybookUrl = import.meta.env.__STORYBOOK_URL__;
3333
const storyUrl = `${storybookUrl}/?path=/story/${meta.storyId}&addonPanel=storybook/interactions/panel`;
3434
currentError.message = `\n\x1B[34mClick to debug the error directly in Storybook: ${storyUrl}\x1B[39m\n\n${currentError.message}`;
3535
}
3636
};
3737

38-
afterAll((suite) => {
39-
suite.tasks.forEach(modifyErrorMessage);
40-
});
38+
afterEach(modifyErrorMessage);

code/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"storybook:ui": "NODE_OPTIONS=\"--preserve-symlinks --preserve-symlinks-main\" ./lib/cli/bin/index.cjs dev --port 6006 --config-dir ./.storybook",
4949
"storybook:ui:build": "NODE_OPTIONS=\"--preserve-symlinks --preserve-symlinks-main\" ./lib/cli/bin/index.cjs build --config-dir ./.storybook --webpack-stats-json",
5050
"storybook:ui:chromatic": "../scripts/node_modules/.bin/chromatic --build-script-name storybook:ui:build --storybook-base-dir ./ --exit-zero-on-changes --exit-once-uploaded",
51-
"storybook:vitest": "yarn test --project storybook-ui",
51+
"storybook:vitest": "yarn test:watch --project storybook-ui",
5252
"storybook:vitest:inspect": "INSPECT=true yarn test --project storybook-ui",
5353
"task": "yarn --cwd ../scripts task",
5454
"test": "NODE_OPTIONS=--max_old_space_size=4096 vitest run",

0 commit comments

Comments
 (0)