Skip to content

Commit abc60a4

Browse files
Fix storybook test for vitest 4.0.16 compatibility
Refactored the storybook.test.tsx to avoid calling describe() inside describe.each() callback. Vitest 4.0.16 introduced stricter rules about when suite functions can be called. The previous pattern used a for loop inside describe.each() to dynamically create nested describe blocks, which is now rejected. Changed to use describe.each() directly with flattened stories array instead of grouping by component name and creating nested describes. Co-authored-by: francinelucca <[email protected]>
1 parent da816c1 commit abc60a4

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

packages/react/src/__tests__/storybook.test.tsx

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import {describe, expect, test} from 'vitest'
44
import glob from 'fast-glob'
5-
import groupBy from 'lodash.groupby'
65

76
const ROOT_DIRECTORY = path.resolve(__dirname, '..', '..')
87

@@ -84,51 +83,47 @@ const stories = await Promise.all(
8483
}),
8584
)
8685

87-
const components = Object.entries(
88-
groupBy(stories, ({name}) => {
89-
return name
90-
}),
91-
)
86+
// Flatten the stories array to work with vitest 4.0.16's stricter suite function rules
87+
const flattenedStories = stories.map(story => ({
88+
testName: `${story.name} - ${story.story.default.title} (${story.relativeFilepath})`,
89+
...story,
90+
}))
9291

93-
describe.each(components)('%s', (_component, stories) => {
94-
for (const {story, type, relativeFilepath} of stories) {
95-
describe(`${story.default.title} (${relativeFilepath})`, () => {
96-
test('title follows naming convention', () => {
97-
// Matches:
98-
// - title: 'Components/TreeView'
99-
// Does not match:
100-
// - title: 'Component/TreeView/Features'
101-
// - title: 'TreeView'
102-
const defaultTitlePattern = /Components\/\w+/
92+
describe.each(flattenedStories)('$testName', ({story, type}) => {
93+
test('title follows naming convention', () => {
94+
// Matches:
95+
// - title: 'Components/TreeView'
96+
// Does not match:
97+
// - title: 'Component/TreeView/Features'
98+
// - title: 'TreeView'
99+
const defaultTitlePattern = /Components\/\w+/
103100

104-
// Matches:
105-
// - title: 'Components/TreeView/Features'
106-
// Does not match:
107-
// - title: 'Component/TreeView'
108-
// - title: 'TreeView'
109-
const featureTitlePattern = /Components\/\w+\/Features/
101+
// Matches:
102+
// - title: 'Components/TreeView/Features'
103+
// Does not match:
104+
// - title: 'Component/TreeView'
105+
// - title: 'TreeView'
106+
const featureTitlePattern = /Components\/\w+\/Features/
110107

111-
expect(story.default.title).toMatch(type === 'default' ? defaultTitlePattern : featureTitlePattern)
112-
})
108+
expect(story.default.title).toMatch(type === 'default' ? defaultTitlePattern : featureTitlePattern)
109+
})
113110

114-
if (type === 'default') {
115-
test('exports a Default story', () => {
116-
expect(story.Default).toBeDefined()
117-
})
111+
if (type === 'default') {
112+
test('exports a Default story', () => {
113+
expect(story.Default).toBeDefined()
114+
})
118115

119-
test('Default story does not have `args`', () => {
120-
expect(story.Default.args).not.toBeDefined()
121-
})
116+
test('Default story does not have `args`', () => {
117+
expect(story.Default.args).not.toBeDefined()
118+
})
122119

123-
test('Default story does not have `argTypes`', () => {
124-
expect(story.Default.argTypes).not.toBeDefined()
125-
})
120+
test('Default story does not have `argTypes`', () => {
121+
expect(story.Default.argTypes).not.toBeDefined()
122+
})
126123

127-
test('only exports Default and Playground stories', () => {
128-
for (const storyName of Object.keys(story)) {
129-
expect(/^Default$|^(.*)Playground$|^default$/.test(storyName)).toBe(true)
130-
}
131-
})
124+
test('only exports Default and Playground stories', () => {
125+
for (const storyName of Object.keys(story)) {
126+
expect(/^Default$|^(.*)Playground$|^default$/.test(storyName)).toBe(true)
132127
}
133128
})
134129
}

0 commit comments

Comments
 (0)