@@ -31,6 +31,17 @@ const packageDir = dirname(require.resolve('@storybook/addon-vitest/package.json
31
31
// We have to tell Vitest that it runs as part of Storybook
32
32
process . env . VITEST_STORYBOOK = 'true' ;
33
33
34
+ /**
35
+ * The Storybook vitest plugin adds double space characters so that it's possible to do a regex for
36
+ * all test run use cases. Otherwise, if there were two unrelated stories like "Primary Button" and
37
+ * "Primary Button Mobile", once you run tests for "Primary Button" and its children it would also
38
+ * match "Primary Button Mobile". As it turns out, this limitation is also present in the Vitest
39
+ * VSCode extension and the issue would occur with normal vitest tests as well, but because we use
40
+ * double spaces, we circumvent the issue.
41
+ */
42
+ export const DOUBLE_SPACES = ' ' ;
43
+ const getTestName = ( name : string ) => `${ name } ${ DOUBLE_SPACES } ` ;
44
+
34
45
export class VitestManager {
35
46
vitest : Vitest | null = null ;
36
47
@@ -271,10 +282,6 @@ export class VitestManager {
271
282
? allStories . filter ( ( story ) => runPayload . storyIds ?. includes ( story . id ) )
272
283
: allStories ;
273
284
274
- // When we run tests that involved a describe block (parent story + tests)
275
- // we need to remove spaces from the parent story name, as the describe block does not have spaces
276
- const getExportName = ( name : string ) => name . replace ( / \s + / g, '' ) ;
277
-
278
285
const isSingleStoryRun = runPayload . storyIds ?. length === 1 ;
279
286
if ( isSingleStoryRun ) {
280
287
const selectedStory = filteredStories . find ( ( story ) => story . id === runPayload . storyIds ?. [ 0 ] ) ;
@@ -291,31 +298,20 @@ export class VitestManager {
291
298
if ( isParentStory ) {
292
299
// Use case 1: "Single" story run on a story with tests
293
300
// -> run all tests of that story, as storyName is a describe block
294
- /**
295
- * The vitest transformation keeps the export name as is, e.g. "PrimaryButton", while the
296
- * storybook sidebar changes the name to "Primary Button". That's why we need to remove
297
- * spaces from the story name, to match the test name. If we were to also beautify the test
298
- * name, doing a regex wouldn't be precise because there could be two describes, for
299
- * instance: "Primary Button" and "Primary Button Mobile" and both would match. The fact
300
- * that there are no spaces in the test name is what makes "PrimaryButton" and
301
- * "PrimaryButtonMobile" work well in the regex. As it turns out, this limitation is also
302
- * present in the Vitest VSCode extension and the issue would occur with normal vitest tests
303
- * as well.
304
- */
305
- const parentName = getExportName ( selectedStory . name ) ;
306
- regex = new RegExp ( `^${ parentName } ` ) ; // the extra space is intentional!
301
+ const parentName = getTestName ( selectedStory . name ) ;
302
+ regex = new RegExp ( `^${ parentName } ` ) ;
307
303
} else if ( hasParentStory ) {
308
304
// Use case 2: Single story run on a specific story test
309
- // in this case the regex pattern should be the story parentName + story.name
305
+ // in this case the regex pattern should be the story parentName + space + story.name
310
306
const parentStory = allStories . find ( ( story ) => story . id === selectedStory . parent ) ;
311
307
if ( ! parentStory ) {
312
308
throw new Error ( `Parent story not found for story ${ selectedStory . id } ` ) ;
313
309
}
314
310
315
- const parentName = getExportName ( parentStory . name ) ;
311
+ const parentName = getTestName ( parentStory . name ) ;
316
312
regex = new RegExp ( `^${ parentName } ${ storyName } $` ) ;
317
313
} else {
318
- // Use case 3: Single story run on a story without tests
314
+ // Use case 3: Single story run on a story without tests, should be exact match of story name
319
315
regex = new RegExp ( `^${ storyName } $` ) ;
320
316
}
321
317
this . vitest ! . setGlobalTestNamePattern ( regex ) ;
0 commit comments