@@ -2,52 +2,55 @@ import { describe, expect, it } from "vitest";
22import { ArgosReporter } from "@argos-ci/vitest/plugin" ;
33import { argosVitestPlugin } from "./vitest-plugin" ;
44
5- function configure ( setupFiles : string [ ] ) {
6- const plugin = argosVitestPlugin ( { uploadToArgos : false } ) as any ;
7- const project = { config : { setupFiles } } ;
8- plugin . configureVitest ( { vitest : { config : { reporters : [ ] } } , project } ) ;
9- return project . config . setupFiles as string [ ] ;
5+ const USER_SETUP_FILE = "/project/.storybook/vitest.setup.ts" ;
6+
7+ /**
8+ * Run the plugin's `configureVitest` hook for `projects` projects of the same
9+ * Vitest run, the way Vitest does for each browser of `browser.instances`.
10+ */
11+ function configure (
12+ options : { uploadToArgos ?: boolean ; projects ?: number } = { } ,
13+ ) {
14+ const { uploadToArgos = false , projects = 1 } = options ;
15+ const plugin = argosVitestPlugin ( { uploadToArgos } ) as any ;
16+ const vitest = { config : { reporters : [ ] as unknown [ ] } } ;
17+ // Each project has its own setup files; all of them end up the same.
18+ let setupFiles : string [ ] = [ ] ;
19+ for ( let index = 0 ; index < projects ; index ++ ) {
20+ setupFiles = [ USER_SETUP_FILE ] ;
21+ plugin . configureVitest ( { vitest, project : { config : { setupFiles } } } ) ;
22+ }
23+ return { setupFiles, reporters : vitest . config . reporters } ;
1024}
1125
1226describe ( "argosVitestPlugin" , ( ) => {
1327 it ( "registers the channel setup ahead of the user's setup files" , ( ) => {
14- const setupFiles = configure ( [ "/project/.storybook/vitest.setup.ts" ] ) ;
28+ const { setupFiles } = configure ( ) ;
1529
1630 // Addon preview modules capture a channel when the user's setup file
1731 // imports them, so ours has to install one first.
1832 expect ( setupFiles [ 0 ] ) . toMatch ( / v i t e s t - s e t u p - c h a n n e l - f i l e \. m j s $ / ) ;
19- expect ( setupFiles ) . toContain ( "/project/.storybook/vitest.setup.ts" ) ;
33+ expect ( setupFiles ) . toContain ( USER_SETUP_FILE ) ;
2034 } ) ;
2135
2236 it ( "registers the screenshot setup after the user's setup files" , ( ) => {
23- const setupFiles = configure ( [ "/project/.storybook/vitest.setup.ts" ] ) ;
37+ const { setupFiles } = configure ( ) ;
2438
2539 // `afterEach` hooks run in reverse registration order, so registering last
2640 // is what makes the screenshot happen before Storybook unmounts the story.
2741 expect ( setupFiles . at ( - 1 ) ) . toMatch ( / v i t e s t - s e t u p - f i l e \. m j s $ / ) ;
2842 } ) ;
2943
3044 it ( "adds the Argos reporter once for all the browser instances" , ( ) => {
31- const plugin = argosVitestPlugin ( { uploadToArgos : true } ) as any ;
32- const vitest = { config : { reporters : [ ] as unknown [ ] } } ;
33- // Vitest runs each browser of `browser.instances` as a project of its own,
34- // and calls `configureVitest` for each of them.
35- for ( const browser of [ "chromium" , "firefox" , "webkit" ] ) {
36- plugin . configureVitest ( {
37- vitest,
38- project : { name : browser , config : { setupFiles : [ ] } } ,
39- } ) ;
40- }
41-
42- expect ( vitest . config . reporters ) . toHaveLength ( 1 ) ;
43- expect ( vitest . config . reporters [ 0 ] ) . toBeInstanceOf ( ArgosReporter ) ;
45+ const { reporters } = configure ( { uploadToArgos : true , projects : 3 } ) ;
46+
47+ expect ( reporters ) . toHaveLength ( 1 ) ;
48+ expect ( reporters [ 0 ] ) . toBeInstanceOf ( ArgosReporter ) ;
4449 } ) ;
4550
4651 it ( "adds no reporter when not uploading to Argos" , ( ) => {
47- const plugin = argosVitestPlugin ( { uploadToArgos : false } ) as any ;
48- const vitest = { config : { reporters : [ ] as unknown [ ] } } ;
49- plugin . configureVitest ( { vitest, project : { config : { setupFiles : [ ] } } } ) ;
52+ const { reporters } = configure ( ) ;
5053
51- expect ( vitest . config . reporters ) . toHaveLength ( 0 ) ;
54+ expect ( reporters ) . toHaveLength ( 0 ) ;
5255 } ) ;
5356} ) ;
0 commit comments