diff --git a/packages/vitest/src/node/pools/forks.ts b/packages/vitest/src/node/pools/forks.ts index 0f558dd788ba..bfc739cc5503 100644 --- a/packages/vitest/src/node/pools/forks.ts +++ b/packages/vitest/src/node/pools/forks.ts @@ -29,7 +29,7 @@ function createChildProcessChannel(project: WorkspaceProject) { postMessage: message => emitter.emit(events.response, message), } - const rpc = createBirpc(createMethodsRPC(project), { + const rpc = createBirpc(createMethodsRPC(project, { cacheFs: true }), { eventNames: ['onCancel'], serialize: v8.serialize, deserialize: v => v8.deserialize(Buffer.from(v)), diff --git a/packages/vitest/src/node/pools/rpc.ts b/packages/vitest/src/node/pools/rpc.ts index 452b3bb3d088..3ce3324bb27e 100644 --- a/packages/vitest/src/node/pools/rpc.ts +++ b/packages/vitest/src/node/pools/rpc.ts @@ -8,8 +8,13 @@ import type { WorkspaceProject } from '../workspace' const created = new Set() const promises = new Map>() -export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC { +interface MethodsOptions { + cacheFs?: boolean +} + +export function createMethodsRPC(project: WorkspaceProject, options: MethodsOptions = {}): RuntimeRPC { const ctx = project.ctx + const cacheFs = options.cacheFs ?? false return { snapshotSaved(snapshot) { ctx.snapshot.add(snapshot) @@ -30,7 +35,7 @@ export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC { async fetch(id, transformMode) { const result = await project.vitenode.fetchResult(id, transformMode) const code = result.code - if (result.externalize) { + if (!cacheFs || result.externalize) { return result } if ('id' in result && typeof result.id === 'string') { diff --git a/packages/vitest/src/node/pools/vmForks.ts b/packages/vitest/src/node/pools/vmForks.ts index 636698a2a28c..5c55a3181311 100644 --- a/packages/vitest/src/node/pools/vmForks.ts +++ b/packages/vitest/src/node/pools/vmForks.ts @@ -34,20 +34,23 @@ function createChildProcessChannel(project: WorkspaceProject) { postMessage: message => emitter.emit(events.response, message), } - const rpc = createBirpc(createMethodsRPC(project), { - eventNames: ['onCancel'], - serialize: v8.serialize, - deserialize: v => v8.deserialize(Buffer.from(v)), - post(v) { - emitter.emit(events.message, v) + const rpc = createBirpc( + createMethodsRPC(project, { cacheFs: true }), + { + eventNames: ['onCancel'], + serialize: v8.serialize, + deserialize: v => v8.deserialize(Buffer.from(v)), + post(v) { + emitter.emit(events.message, v) + }, + on(fn) { + emitter.on(events.response, fn) + }, + onTimeoutError(functionName) { + throw new Error(`[vitest-pool]: Timeout calling "${functionName}"`) + }, }, - on(fn) { - emitter.on(events.response, fn) - }, - onTimeoutError(functionName) { - throw new Error(`[vitest-pool]: Timeout calling "${functionName}"`) - }, - }) + ) project.ctx.onCancel(reason => rpc.onCancel(reason))