11// Vitest performance config tests validate performance test project setup.
2- import { describe , expect , it } from "vitest" ;
2+ import { spawnSync } from "node:child_process" ;
3+ import fs from "node:fs" ;
4+ import { createRequire } from "node:module" ;
5+ import path from "node:path" ;
6+ import { afterEach , describe , expect , it } from "vitest" ;
7+ import { useAutoCleanupTempDirTracker } from "./helpers/temp-dir.js" ;
38import { loadVitestExperimentalConfig } from "./vitest/vitest.performance-config.ts" ;
49
510describe ( "loadVitestExperimentalConfig" , ( ) => {
611 it ( "enables the filesystem module cache by default" , ( ) => {
712 expect ( loadVitestExperimentalConfig ( { } , "linux" ) ) . toEqual ( {
813 experimental : {
914 fsModuleCache : true ,
15+ fsModuleCachePath : path . join ( process . cwd ( ) , ".cache" , "vitest" , "default" ) ,
1016 } ,
1117 } ) ;
1218 } ) ;
@@ -22,6 +28,7 @@ describe("loadVitestExperimentalConfig", () => {
2228 ) . toEqual ( {
2329 experimental : {
2430 fsModuleCache : true ,
31+ fsModuleCachePath : path . join ( process . cwd ( ) , ".cache" , "vitest" , "default" ) ,
2532 } ,
2633 } ) ;
2734 } ) ;
@@ -57,6 +64,7 @@ describe("loadVitestExperimentalConfig", () => {
5764 ) . toEqual ( {
5865 experimental : {
5966 fsModuleCache : true ,
67+ fsModuleCachePath : path . join ( process . cwd ( ) , ".cache" , "vitest" , "default" ) ,
6068 } ,
6169 } ) ;
6270 } ) ;
@@ -84,6 +92,7 @@ describe("loadVitestExperimentalConfig", () => {
8492 ) . toEqual ( {
8593 experimental : {
8694 fsModuleCache : true ,
95+ fsModuleCachePath : path . join ( process . cwd ( ) , ".cache" , "vitest" , "default" ) ,
8796 importDurations : { print : true } ,
8897 printImportBreakdown : true ,
8998 } ,
@@ -94,3 +103,73 @@ describe("loadVitestExperimentalConfig", () => {
94103 expect ( loadVitestExperimentalConfig ( { RUNNER_OS : "Windows" } , "linux" ) ) . toStrictEqual ( { } ) ;
95104 } ) ;
96105} ) ;
106+
107+ describe ( "filesystem module cache ownership" , ( ) => {
108+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
109+
110+ it ( "preserves another checkout's cache when shared dependencies change" , ( ) => {
111+ const root = tempDirs . make ( "oc-vitest-cache-ownership-" ) ;
112+ const sharedModules = path . join ( root , "shared" , "node_modules" ) ;
113+ fs . mkdirSync ( path . join ( sharedModules , ".pnpm" ) , { recursive : true } ) ;
114+ const lockfile = path . join ( sharedModules , ".pnpm" , "lock.yaml" ) ;
115+ fs . writeFileSync ( lockfile , "lockfileVersion: 1\n" ) ;
116+ const cli = path . join (
117+ path . dirname ( createRequire ( import . meta. url ) . resolve ( "vitest/package.json" ) ) ,
118+ "vitest.mjs" ,
119+ ) ;
120+ const prepareCheckout = ( name : string ) => {
121+ const checkout = path . join ( root , name ) ;
122+ fs . mkdirSync ( checkout ) ;
123+ // Stop Vite's workspace search before it reaches this repository. The
124+ // symlink points only at this fixture's dependencies and writable cache.
125+ fs . writeFileSync (
126+ path . join ( checkout , "package.json" ) ,
127+ JSON . stringify ( { name, type : "module" , workspaces : [ ] } ) ,
128+ ) ;
129+ fs . symlinkSync ( sharedModules , path . join ( checkout , "node_modules" ) , "junction" ) ;
130+ fs . writeFileSync (
131+ path . join ( checkout , "fixture.test.js" ) ,
132+ 'test("runs the fixture", () => expect(2 + 2).toBe(4));\n' ,
133+ ) ;
134+ const cacheConfig = loadVitestExperimentalConfig ( { } , "linux" , checkout ) ;
135+ const config = {
136+ root : checkout ,
137+ test : {
138+ globals : true ,
139+ include : [ "fixture.test.js" ] ,
140+ maxWorkers : 1 ,
141+ ...cacheConfig ,
142+ } ,
143+ } ;
144+ fs . writeFileSync (
145+ path . join ( checkout , "vitest.config.mjs" ) ,
146+ `export default ${ JSON . stringify ( config ) } ;\n` ,
147+ ) ;
148+ return { checkout, cacheConfig } ;
149+ } ;
150+ const run = ( checkout : string ) => {
151+ const result = spawnSync (
152+ process . execPath ,
153+ [ cli , "run" , "--config" , path . join ( checkout , "vitest.config.mjs" ) ] ,
154+ {
155+ cwd : checkout ,
156+ encoding : "utf8" ,
157+ env : { PATH : process . env . PATH , SystemRoot : process . env . SystemRoot , CI : "1" , HOME : root } ,
158+ timeout : 15_000 ,
159+ } ,
160+ ) ;
161+ expect ( result . status , `${ result . error ?? "" } \n${ result . stdout } \n${ result . stderr } ` ) . toBe ( 0 ) ;
162+ } ;
163+ const first = prepareCheckout ( "first" ) ;
164+ const second = prepareCheckout ( "second" ) ;
165+ run ( first . checkout ) ;
166+ const firstCache =
167+ first . cacheConfig . experimental ?. fsModuleCachePath ??
168+ path . join ( sharedModules , ".experimental-vitest-cache" ) ;
169+ const sentinel = path . join ( firstCache , "first-checkout-sentinel" ) ;
170+ fs . writeFileSync ( sentinel , "owned by first checkout" ) ;
171+ fs . writeFileSync ( lockfile , "lockfileVersion: 2\n" ) ;
172+ run ( second . checkout ) ;
173+ expect ( fs . readFileSync ( sentinel , "utf8" ) ) . toBe ( "owned by first checkout" ) ;
174+ } ) ;
175+ } ) ;
0 commit comments