Skip to content

Commit 53d295d

Browse files
committed
test and improvements
1 parent 3646ab5 commit 53d295d

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed
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+
});

code/addons/vitest/src/plugin/setup-file.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

@@ -13,13 +13,15 @@ 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-
const modifyErrorMessage = ({ task }: { task: RunnerTask }) => {
22-
const meta = task.meta as ExtendedMeta;
23+
export const modifyErrorMessage = ({ task }: { task: Task }) => {
24+
const meta = task.meta;
2325
if (
2426
task.type === 'test' &&
2527
task.result?.state === 'fail' &&

0 commit comments

Comments
 (0)