@@ -2,7 +2,7 @@ import fs from 'node:fs'
2
2
import { dirname , resolve } from 'node:path'
3
3
import { fileURLToPath } from 'node:url'
4
4
import type { TestAPI } from 'vitest'
5
- import { afterEach , beforeEach , test } from 'vitest'
5
+ import { afterEach , beforeEach , onTestFinished , test } from 'vitest'
6
6
import type { ModuleRunner } from 'vite/module-runner'
7
7
import type { ServerModuleRunnerOptions } from '../serverModuleRunner'
8
8
import type { ViteDevServer } from '../../../server'
@@ -103,32 +103,26 @@ export async function createModuleRunnerTester(
103
103
return test as TestAPI < TestClient >
104
104
}
105
105
106
- const originalFiles = new Map < string , string > ( )
107
- const createdFiles = new Set < string > ( )
108
- afterEach ( ( ) => {
109
- originalFiles . forEach ( ( content , file ) => {
110
- fs . writeFileSync ( file , content , 'utf-8' )
111
- } )
112
- createdFiles . forEach ( ( file ) => {
113
- if ( fs . existsSync ( file ) ) fs . unlinkSync ( file )
114
- } )
115
- originalFiles . clear ( )
116
- createdFiles . clear ( )
117
- } )
118
-
119
- export function createFile ( file : string , content : string ) : void {
120
- createdFiles . add ( file )
121
- fs . mkdirSync ( dirname ( file ) , { recursive : true } )
122
- fs . writeFileSync ( file , content , 'utf-8' )
106
+ type FixtureEditor = {
107
+ editFile : ( file : string , callback : ( content : string ) => string ) => void
123
108
}
124
109
125
- export function editFile (
126
- file : string ,
127
- callback : ( content : string ) => string ,
128
- ) : void {
129
- const content = fs . readFileSync ( file , 'utf-8' )
130
- if ( ! originalFiles . has ( file ) ) originalFiles . set ( file , content )
131
- fs . writeFileSync ( file , callback ( content ) , 'utf-8' )
110
+ export function createFixtureEditor ( ) : FixtureEditor {
111
+ const originalFiles = new Map < string , string > ( )
112
+ onTestFinished ( ( ) => {
113
+ originalFiles . forEach ( ( content , file ) => {
114
+ fs . writeFileSync ( file , content , 'utf-8' )
115
+ } )
116
+ originalFiles . clear ( )
117
+ } )
118
+
119
+ return {
120
+ editFile ( file , callback ) {
121
+ const content = fs . readFileSync ( file , 'utf-8' )
122
+ if ( ! originalFiles . has ( file ) ) originalFiles . set ( file , content )
123
+ fs . writeFileSync ( file , callback ( content ) , 'utf-8' )
124
+ } ,
125
+ }
132
126
}
133
127
134
128
export function resolvePath ( baseUrl : string , path : string ) : string {
0 commit comments