Skip to content

Commit 2565570

Browse files
yannbfstorybook-bot
authored andcommitted
Merge pull request #29147 from InfiniteXyy/next
Addon Test: Support story name as test description (cherry picked from commit 0811ca8)
1 parent 1d27107 commit 2565570

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

code/core/src/csf-tools/vitest-plugin/transformer.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,55 @@ describe('transformer', () => {
213213
`);
214214
});
215215

216+
describe("use the story's name as test title", () => {
217+
it('should support CSF v3 via name property', async () => {
218+
const code = `
219+
export default { component: Button }
220+
export const Primary = { name: "custom name" };`;
221+
const result = await transform({ code });
222+
223+
expect(result.code).toMatchInlineSnapshot(`
224+
import { test as _test, expect as _expect } from "vitest";
225+
import { testStory as _testStory } from "@storybook/experimental-addon-test/internal/test-utils";
226+
const _meta = {
227+
component: Button,
228+
title: "automatic/calculated/title"
229+
};
230+
export default _meta;
231+
export const Primary = {
232+
name: "custom name"
233+
};
234+
const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath);
235+
if (_isRunningFromThisFile) {
236+
_test("custom name", _testStory("Primary", Primary, _meta, []));
237+
}
238+
`);
239+
});
240+
241+
it('should support CSF v1/v2 via storyName property', async () => {
242+
const code = `
243+
export default { component: Button }
244+
export const Story = () => {}
245+
Story.storyName = 'custom name';`;
246+
const result = await transform({ code: code });
247+
expect(result.code).toMatchInlineSnapshot(`
248+
import { test as _test, expect as _expect } from "vitest";
249+
import { testStory as _testStory } from "@storybook/experimental-addon-test/internal/test-utils";
250+
const _meta = {
251+
component: Button,
252+
title: "automatic/calculated/title"
253+
};
254+
export default _meta;
255+
export const Story = () => {};
256+
Story.storyName = 'custom name';
257+
const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath);
258+
if (_isRunningFromThisFile) {
259+
_test("custom name", _testStory("Story", Story, _meta, []));
260+
}
261+
`);
262+
});
263+
});
264+
216265
it('should add test statement to const declared exported stories', async () => {
217266
const code = `
218267
export default {};

code/core/src/csf-tools/vitest-plugin/transformer.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,17 @@ export async function vitestTransform({
202202

203203
const getTestStatementForStory = ({
204204
exportName,
205+
testTitle,
205206
node,
206207
}: {
207208
exportName: string;
209+
testTitle: string;
208210
node: t.Node;
209211
}): t.ExpressionStatement => {
210212
// Create the _test expression directly using the exportName identifier
211213
const testStoryCall = t.expressionStatement(
212214
t.callExpression(vitestTestId, [
213-
t.stringLiteral(exportName),
215+
t.stringLiteral(testTitle),
214216
t.callExpression(testStoryId, [
215217
t.stringLiteral(exportName),
216218
t.identifier(exportName),
@@ -239,10 +241,9 @@ export async function vitestTransform({
239241
return;
240242
}
241243

242-
return getTestStatementForStory({
243-
exportName,
244-
node,
245-
});
244+
// use the story's name as the test title for vitest, and fallback to exportName
245+
const testTitle = parsed._stories[exportName].name ?? exportName;
246+
return getTestStatementForStory({ testTitle, exportName, node });
246247
})
247248
.filter((st) => !!st) as t.ExpressionStatement[];
248249

docs/writing-tests/vitest-plugin.mdx

+10
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,16 @@ We recommend running tests in a browser using Playwright, but you can use WebDri
379379

380380
We recommend using Chromium, because it is most likely to best match the experience of a majority of your users. However, you can use other browsers by adjusting the [browser name in the Vitest configuration file](https://vitest.dev/config/#browser-name). Note that [Playwright and WebDriverIO support different browsers](https://vitest.dev/guide/browser/#browser-option-types).
381381

382+
### How do I customize a test name?
383+
384+
By default, the export name of a story is mapped to the test name. To create a more descriptive test description, you can provide a `name` property for the story. This allows you to include spaces, brackets, or other special characters.
385+
386+
```js
387+
export const Story = {
388+
name: 'custom, descriptive name'
389+
};
390+
```
391+
382392
## API
383393

384394
### Exports

0 commit comments

Comments
 (0)