Skip to content

Commit ed00bc5

Browse files
authored
Merge pull request #29250 from storybookjs/shilman/29244-vitest-renamed-exports
Vitest plugin: Fix renamed export stories
2 parents 5aaf034 + d23542f commit ed00bc5

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

code/core/src/csf-tools/CsfFile.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('CsfFile', () => {
8181
stories:
8282
- id: foo-bar--a
8383
name: A
84+
localName: A
8485
parameters:
8586
__id: foo-bar--a
8687
__stats:
@@ -94,6 +95,7 @@ describe('CsfFile', () => {
9495
moduleMock: false
9596
- id: foo-bar--b
9697
name: B
98+
localName: B
9799
parameters:
98100
__id: foo-bar--b
99101
__stats:
@@ -790,6 +792,7 @@ describe('CsfFile', () => {
790792
stories:
791793
- id: foo-bar--a
792794
name: A
795+
localName: default
793796
__stats:
794797
play: false
795798
render: false
@@ -801,6 +804,7 @@ describe('CsfFile', () => {
801804
moduleMock: false
802805
- id: foo-bar--b
803806
name: B
807+
localName: B
804808
__stats:
805809
play: false
806810
render: false
@@ -878,6 +882,7 @@ describe('CsfFile', () => {
878882
stories:
879883
- id: foo-bar--a
880884
name: A
885+
localName: A
881886
parameters:
882887
__id: foo-bar--a
883888
__stats:

code/core/src/csf-tools/CsfFile.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export interface StaticMeta
190190

191191
export interface StaticStory extends Pick<StoryAnnotations, 'name' | 'parameters' | 'tags'> {
192192
id: string;
193+
localName?: string;
193194
__stats: IndexInputStats;
194195
}
195196

@@ -488,6 +489,7 @@ export class CsfFile {
488489
node.specifiers.forEach((specifier) => {
489490
if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) {
490491
const { name: exportName } = specifier.exported;
492+
const { name: localName } = specifier.local;
491493
const decl = t.isProgram(parent)
492494
? findVarInitialization(specifier.local.name, parent)
493495
: specifier.local;
@@ -515,6 +517,7 @@ export class CsfFile {
515517
self._stories[exportName] = {
516518
id: 'FIXME',
517519
name: exportName,
520+
localName,
518521
parameters: {},
519522
__stats: {},
520523
};

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,40 @@ describe('transformer', () => {
296296
`);
297297
});
298298

299+
it('should add test statement to const declared renamed exported stories', async () => {
300+
const code = `
301+
export default {};
302+
const Primary = {
303+
args: {
304+
label: 'Primary Button',
305+
},
306+
};
307+
308+
export { Primary as PrimaryStory };
309+
`;
310+
311+
const result = await transform({ code });
312+
313+
expect(result.code).toMatchInlineSnapshot(`
314+
import { test as _test, expect as _expect } from "vitest";
315+
import { testStory as _testStory } from "@storybook/experimental-addon-test/internal/test-utils";
316+
const _meta = {
317+
title: "automatic/calculated/title"
318+
};
319+
export default _meta;
320+
const Primary = {
321+
args: {
322+
label: 'Primary Button'
323+
}
324+
};
325+
export { Primary as PrimaryStory };
326+
const _isRunningFromThisFile = import.meta.url.includes(globalThis.__vitest_worker__.filepath ?? _expect.getState().testPath);
327+
if (_isRunningFromThisFile) {
328+
_test("PrimaryStory", _testStory("PrimaryStory", Primary, _meta, []));
329+
}
330+
`);
331+
});
332+
299333
it('should add tests for multiple stories', async () => {
300334
const code = `
301335
export default {};

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,12 @@ export async function vitestTransform({
201201
ast.program.body.push(isRunningFromThisFileDeclaration);
202202

203203
const getTestStatementForStory = ({
204+
localName,
204205
exportName,
205206
testTitle,
206207
node,
207208
}: {
209+
localName: string;
208210
exportName: string;
209211
testTitle: string;
210212
node: t.Node;
@@ -215,7 +217,7 @@ export async function vitestTransform({
215217
t.stringLiteral(testTitle),
216218
t.callExpression(testStoryId, [
217219
t.stringLiteral(exportName),
218-
t.identifier(exportName),
220+
t.identifier(localName),
219221
t.identifier(metaExportName),
220222
skipTagsId,
221223
]),
@@ -241,9 +243,10 @@ export async function vitestTransform({
241243
return;
242244
}
243245

246+
const localName = parsed._stories[exportName].localName ?? exportName;
244247
// use the story's name as the test title for vitest, and fallback to exportName
245248
const testTitle = parsed._stories[exportName].name ?? exportName;
246-
return getTestStatementForStory({ testTitle, exportName, node });
249+
return getTestStatementForStory({ testTitle, localName, exportName, node });
247250
})
248251
.filter((st) => !!st) as t.ExpressionStatement[];
249252

0 commit comments

Comments
 (0)