Skip to content

Commit 82d9315

Browse files
authored
test: onTestFinished instead of afterEach for file restoration (#221)
1 parent 8b3e367 commit 82d9315

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect } from 'vitest'
22
import type { ViteDevServer } from '../../..'
3-
import { createModuleRunnerTester, editFile, resolvePath } from './utils'
3+
import {
4+
createFixtureEditor,
5+
createModuleRunnerTester,
6+
resolvePath,
7+
} from './utils'
48

59
describe('module runner initialization', async () => {
610
const it = await createModuleRunnerTester(
@@ -47,8 +51,10 @@ describe('module runner initialization', async () => {
4751
' at Module.throwError (<root>/fixtures/throws-error-method.ts:6:9)',
4852
)
4953

54+
const fixtureEditor = createFixtureEditor()
55+
5056
// simulate HMR
51-
editFile(
57+
fixtureEditor.editFile(
5258
resolvePath(import.meta.url, './fixtures/throws-error-method.ts'),
5359
(code) => '\n\n\n\n\n' + code + '\n',
5460
)

packages/vite/src/node/ssr/runtime/__tests__/utils.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs'
22
import { dirname, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import type { TestAPI } from 'vitest'
5-
import { afterEach, beforeEach, test } from 'vitest'
5+
import { afterEach, beforeEach, onTestFinished, test } from 'vitest'
66
import type { ModuleRunner } from 'vite/module-runner'
77
import type { ServerModuleRunnerOptions } from '../serverModuleRunner'
88
import type { ViteDevServer } from '../../../server'
@@ -103,32 +103,26 @@ export async function createModuleRunnerTester(
103103
return test as TestAPI<TestClient>
104104
}
105105

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
123108
}
124109

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+
}
132126
}
133127

134128
export function resolvePath(baseUrl: string, path: string): string {

0 commit comments

Comments
 (0)