@@ -43,6 +43,19 @@ vi.mock("fs", () => {
4343
4444console . warn = vi . fn ( ) ;
4545
46+ const EXPECTED_EXEC_ARGV = [
47+ "--interpreted-frames-native-stack" ,
48+ "--allow-natives-syntax" ,
49+ "--hash-seed=1" ,
50+ "--random-seed=1" ,
51+ "--no-opt" ,
52+ "--predictable" ,
53+ "--predictable-gc-schedule" ,
54+ "--expose-gc" ,
55+ "--no-concurrent-sweeping" ,
56+ "--max-old-space-size=4096" ,
57+ ] ;
58+
4659describe ( "codSpeedPlugin" , ( ) => {
4760 beforeAll ( ( ) => {
4861 // Set environment variables to trigger instrumented mode
@@ -54,6 +67,7 @@ describe("codSpeedPlugin", () => {
5467 // Clean up environment variables
5568 delete process . env . CODSPEED_ENV ;
5669 delete process . env . CODSPEED_RUNNER_MODE ;
70+ fsMocks . setMockVersion ( "4.0.18" ) ;
5771 } ) ;
5872
5973 it ( "should have a name" , async ( ) => {
@@ -65,7 +79,9 @@ describe("codSpeedPlugin", () => {
6579 } ) ;
6680
6781 describe ( "apply" , ( ) => {
68- it ( "should not apply the plugin when the mode is not benchmark" , async ( ) => {
82+ it ( "should not apply the plugin when the mode is not benchmark (v3/v4)" , async ( ) => {
83+ fsMocks . setMockVersion ( "4.0.18" ) ;
84+
6985 const applyPlugin = applyPluginFunction (
7086 { } ,
7187 fromPartial ( { mode : "test" } ) ,
@@ -74,7 +90,8 @@ describe("codSpeedPlugin", () => {
7490 expect ( applyPlugin ) . toBe ( false ) ;
7591 } ) ;
7692
77- it ( "should apply the plugin when there is no instrumentation" , async ( ) => {
93+ it ( "should apply the plugin when there is no instrumentation (v3/v4)" , async ( ) => {
94+ fsMocks . setMockVersion ( "4.0.18" ) ;
7895 coreMocks . InstrumentHooks . isInstrumented . mockReturnValue ( false ) ;
7996
8097 const applyPlugin = applyPluginFunction (
@@ -88,7 +105,8 @@ describe("codSpeedPlugin", () => {
88105 expect ( applyPlugin ) . toBe ( true ) ;
89106 } ) ;
90107
91- it ( "should apply the plugin when there is instrumentation" , async ( ) => {
108+ it ( "should apply the plugin when there is instrumentation (v3/v4)" , async ( ) => {
109+ fsMocks . setMockVersion ( "4.0.18" ) ;
92110 coreMocks . InstrumentHooks . isInstrumented . mockReturnValue ( true ) ;
93111
94112 const applyPlugin = applyPluginFunction (
@@ -98,33 +116,40 @@ describe("codSpeedPlugin", () => {
98116
99117 expect ( applyPlugin ) . toBe ( true ) ;
100118 } ) ;
119+
120+ it ( "should stay active regardless of mode on v5 (benchmark gating happens in config)" , async ( ) => {
121+ fsMocks . setMockVersion ( "5.0.0-beta.5" ) ;
122+ coreMocks . InstrumentHooks . isInstrumented . mockReturnValue ( true ) ;
123+
124+ const applyPlugin = applyPluginFunction (
125+ { } ,
126+ fromPartial ( { mode : "test" } ) ,
127+ ) ;
128+
129+ expect ( applyPlugin ) . toBe ( true ) ;
130+ fsMocks . setMockVersion ( "4.0.18" ) ;
131+ } ) ;
101132 } ) ;
102133
103134 it ( "should apply the codspeed config for v4" , ( ) => {
135+ fsMocks . setMockVersion ( "4.0.18" ) ;
104136 const config = resolvedCodSpeedPlugin . config ;
105137 if ( typeof config !== "function" )
106138 throw new Error ( "config is not a function" ) ;
107139
108- const result = config . call ( { } as never , { } , fromPartial ( { } ) ) ;
140+ const result = config . call (
141+ { } as never ,
142+ { } ,
143+ fromPartial ( { mode : "benchmark" } ) ,
144+ ) ;
109145
110146 expect ( result ) . toStrictEqual ( {
111147 test : {
112148 globalSetup : [
113149 expect . stringContaining ( "packages/vitest-plugin/src/globalSetup.ts" ) ,
114150 ] ,
115151 pool : "forks" ,
116- execArgv : [
117- "--interpreted-frames-native-stack" ,
118- "--allow-natives-syntax" ,
119- "--hash-seed=1" ,
120- "--random-seed=1" ,
121- "--no-opt" ,
122- "--predictable" ,
123- "--predictable-gc-schedule" ,
124- "--expose-gc" ,
125- "--no-concurrent-sweeping" ,
126- "--max-old-space-size=4096" ,
127- ] ,
152+ execArgv : EXPECTED_EXEC_ARGV ,
128153 runner : expect . stringContaining (
129154 "packages/vitest-plugin/src/analysis.ts" ,
130155 ) ,
@@ -133,16 +158,18 @@ describe("codSpeedPlugin", () => {
133158 } ) ;
134159
135160 it ( "should apply the codspeed config for v3 with poolOptions" , ( ) => {
136- // Set mock version to v3
137161 fsMocks . setMockVersion ( "3.2.0" ) ;
138162
139- // Create a new plugin instance to pick up the mocked version
140163 const v3Plugin = codspeedPlugin ( ) ;
141164 const config = v3Plugin . config ;
142165 if ( typeof config !== "function" )
143166 throw new Error ( "config is not a function" ) ;
144167
145- const result = config . call ( { } as never , { } , fromPartial ( { } ) ) ;
168+ const result = config . call (
169+ { } as never ,
170+ { } ,
171+ fromPartial ( { mode : "benchmark" } ) ,
172+ ) ;
146173
147174 expect ( result ) . toStrictEqual ( {
148175 test : {
@@ -152,18 +179,7 @@ describe("codSpeedPlugin", () => {
152179 pool : "forks" ,
153180 poolOptions : {
154181 forks : {
155- execArgv : [
156- "--interpreted-frames-native-stack" ,
157- "--allow-natives-syntax" ,
158- "--hash-seed=1" ,
159- "--random-seed=1" ,
160- "--no-opt" ,
161- "--predictable" ,
162- "--predictable-gc-schedule" ,
163- "--expose-gc" ,
164- "--no-concurrent-sweeping" ,
165- "--max-old-space-size=4096" ,
166- ] ,
182+ execArgv : EXPECTED_EXEC_ARGV ,
167183 } ,
168184 } ,
169185 runner : expect . stringContaining (
@@ -172,7 +188,55 @@ describe("codSpeedPlugin", () => {
172188 } ,
173189 } ) ;
174190
175- // Reset mock version back to v4
176191 fsMocks . setMockVersion ( "4.0.18" ) ;
177192 } ) ;
193+
194+ describe ( "v5 config" , ( ) => {
195+ it ( "should not inject config when benchmarks are not enabled" , ( ) => {
196+ fsMocks . setMockVersion ( "5.0.0-beta.5" ) ;
197+ const v5Plugin = codspeedPlugin ( ) ;
198+ const config = v5Plugin . config ;
199+ if ( typeof config !== "function" )
200+ throw new Error ( "config is not a function" ) ;
201+
202+ const result = config . call ( { } as never , { } , fromPartial ( { mode : "test" } ) ) ;
203+
204+ expect ( result ) . toBeUndefined ( ) ;
205+ fsMocks . setMockVersion ( "4.0.18" ) ;
206+ } ) ;
207+
208+ it ( "should inject the v5 setup file (not a runner) when benchmarks are enabled" , ( ) => {
209+ fsMocks . setMockVersion ( "5.0.0-beta.5" ) ;
210+ const v5Plugin = codspeedPlugin ( ) ;
211+ const config = v5Plugin . config ;
212+ if ( typeof config !== "function" )
213+ throw new Error ( "config is not a function" ) ;
214+
215+ const result = config . call (
216+ { } as never ,
217+ // `benchmark.enabled` is a Vitest 5 config field the v3/4 typings (which
218+ // this file may be compiled against) don't expose.
219+ { test : { benchmark : { enabled : true } } } as never ,
220+ fromPartial ( { mode : "test" } ) ,
221+ ) ;
222+
223+ expect ( result ) . toStrictEqual ( {
224+ test : {
225+ globalSetup : [
226+ expect . stringContaining (
227+ "packages/vitest-plugin/src/globalSetup.ts" ,
228+ ) ,
229+ ] ,
230+ pool : "forks" ,
231+ execArgv : EXPECTED_EXEC_ARGV ,
232+ setupFiles : [
233+ expect . stringContaining ( "packages/vitest-plugin/src/v5/setup.ts" ) ,
234+ ] ,
235+ } ,
236+ } ) ;
237+ // The v5 path must not set a custom runner.
238+ expect ( ( result as { test ?: { runner ?: unknown } } ) ?. test ?. runner ) . toBeUndefined ( ) ;
239+ fsMocks . setMockVersion ( "4.0.18" ) ;
240+ } ) ;
241+ } ) ;
178242} ) ;
0 commit comments