Skip to content

Commit 6ac71c2

Browse files
authored
Merge pull request #32041 from storybookjs/vitest-transform-plain-stories-tsx
Addon Vitest: Fix support for plain `stories.tsx` files
2 parents edbd7c4 + 86cf4e4 commit 6ac71c2

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

code/.storybook/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ const config = defineMain({
9393
directory: '../addons/vitest/template/stories',
9494
titlePrefix: 'addons/vitest',
9595
},
96+
{
97+
directory: '../addons/vitest/src',
98+
titlePrefix: 'addons/vitest',
99+
files: 'stories.tsx',
100+
},
96101
],
97102
addons: [
98103
'@storybook/addon-themes',

code/addons/vitest/src/stories.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
3+
import type { Meta, StoryObj } from '@storybook/react-vite';
4+
5+
import { expect } from 'storybook/test';
6+
7+
const meta = {
8+
title: 'StoriesTsx',
9+
render: () => <div>This is a story coming from a /stories.tsx file detected via custom glob</div>,
10+
} satisfies Meta;
11+
12+
export default meta;
13+
14+
type Story = StoryObj<typeof meta>;
15+
16+
export const Default: Story = {
17+
play: async () => {
18+
expect(true).toBe(true);
19+
},
20+
};

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ const transform = async ({
4848
};
4949

5050
describe('transformer', () => {
51-
describe('no-op', () => {
52-
it('should return original code if the file is not a story file', async () => {
53-
const code = `console.log('Not a story file');`;
54-
const fileName = 'src/components/Button.js';
55-
56-
const result = await transform({ code, fileName });
57-
58-
expect(result.code).toMatchInlineSnapshot(`console.log('Not a story file');`);
59-
});
60-
});
61-
6251
describe('CSF v1/v2/v3', () => {
6352
describe('default exports (meta)', () => {
6453
it('should add title to inline default export if not present', async () => {
@@ -124,7 +113,7 @@ describe('transformer', () => {
124113
component: Button,
125114
};
126115
export default meta;
127-
116+
128117
export const Story = {};
129118
`;
130119

@@ -153,9 +142,9 @@ describe('transformer', () => {
153142
const meta = {
154143
title: 'Button',
155144
component: Button,
156-
};
145+
};
157146
export default meta;
158-
147+
159148
export const Story = {};
160149
`;
161150

@@ -272,7 +261,7 @@ describe('transformer', () => {
272261
label: 'Primary Button',
273262
},
274263
};
275-
264+
276265
export { Primary };
277266
`;
278267

@@ -306,7 +295,7 @@ describe('transformer', () => {
306295
label: 'Primary Button',
307296
},
308297
};
309-
298+
310299
export { Primary as PrimaryStory };
311300
`;
312301

@@ -340,9 +329,9 @@ describe('transformer', () => {
340329
label: 'Primary Button',
341330
},
342331
};
343-
332+
344333
export const Secondary = {}
345-
334+
346335
export { Primary };
347336
`;
348337

@@ -430,7 +419,7 @@ describe('transformer', () => {
430419
const code = `
431420
export default {};
432421
export const Included = { tags: ['include-me'] };
433-
422+
434423
export const NotIncluded = {}
435424
`;
436425

@@ -461,7 +450,7 @@ describe('transformer', () => {
461450
const code = `
462451
export default {};
463452
export const Included = {};
464-
453+
465454
export const NotIncluded = { tags: ['exclude-me'] }
466455
`;
467456

@@ -636,7 +625,7 @@ describe('transformer', () => {
636625
const code = `
637626
import { config } from '#.storybook/preview';
638627
const meta = config.meta({ component: Button });
639-
const Primary = meta.story({
628+
const Primary = meta.story({
640629
args: {
641630
label: 'Primary Button',
642631
}
@@ -672,7 +661,7 @@ describe('transformer', () => {
672661
const code = `
673662
import { config } from '#.storybook/preview';
674663
const meta = config.meta({ component: Button });
675-
const Primary = meta.story({
664+
const Primary = meta.story({
676665
args: {
677666
label: 'Primary Button',
678667
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ export async function vitestTransform({
4646
stories: StoriesEntry[];
4747
previewLevelTags: Tag[];
4848
}): Promise<ReturnType<typeof formatCsf>> {
49-
const isStoryFile = /\.stor(y|ies)\./.test(fileName);
50-
if (!isStoryFile) {
51-
return code;
52-
}
53-
5449
const parsed = loadCsf(code, {
5550
fileName,
5651
transformInlineMeta: true,

0 commit comments

Comments
 (0)