diff --git a/packages/vitest/src/node/cache/fsModuleCache.ts b/packages/vitest/src/node/cache/fsModuleCache.ts index b1820f4b4..3063a4dda 100644 --- a/packages/vitest/src/node/cache/fsModuleCache.ts +++ b/packages/vitest/src/node/cache/fsModuleCache.ts @@ -146,7 +146,6 @@ export class FileSystemModuleCache { return { id: meta.id, - url: meta.url, file: meta.file, code, imports: meta.imports, @@ -169,7 +168,6 @@ export class FileSystemModuleCache { const result = { file: fetchResult.file, id: fetchResult.id, - url: fetchResult.url, imports, mappings, moduleType: fetchResult.moduleType, @@ -411,7 +409,6 @@ export async function atomicWriteFile(realFilePath: string, data: string): Promi } export interface CachedInlineModuleMeta { - url: string id: string file: string | null code: string diff --git a/packages/vitest/src/node/environments/transformService.ts b/packages/vitest/src/node/environments/transformService.ts index e82ad41b5..3a9cedbc4 100644 --- a/packages/vitest/src/node/environments/transformService.ts +++ b/packages/vitest/src/node/environments/transformService.ts @@ -83,8 +83,10 @@ export class ModuleTransformService { */ async transform(id: string, environment: DevEnvironment): Promise { await this.traces.waitInit() + // the module graph keeps the url of the first request, so it has to be the url that workers import + const url = normalizeResolvedIdToUrl(environment, id) const result = await this.traces.$('vitest.module.transform', (span) => - this.fetchInSpan(span, id, undefined, environment), + this.fetchInSpan(span, url, undefined, environment), ) // externalized modules are loaded by Node without a transform, like in a normal run if ('externalize' in result) { @@ -96,7 +98,7 @@ export class ModuleTransformService { return transformResult } } - return environment.transformRequest(id) + return environment.transformRequest(url) } private async fetchInSpan( @@ -400,7 +402,7 @@ export class ModuleTransformService { file: cachedModule.file, id: cachedModule.id, tmp: cachePath, - url: cachedModule.url, + url: moduleGraphModule.url, invalidate: false, moduleType, } diff --git a/test/e2e/test/caching.test.ts b/test/e2e/test/caching.test.ts index a59b57633..4c0007cb3 100644 --- a/test/e2e/test/caching.test.ts +++ b/test/e2e/test/caching.test.ts @@ -267,6 +267,52 @@ test('filtering by related sources reuses the cache', async () => { delete (globalThis as any).__vitest_transformed__ }) +test('filtering by related sources runs setup files before every non-isolated file', async () => { + const testFile = ` + import { expect, test } from 'vitest' + import './dep.js' + + const setupRuns = globalThis.setupRuns + globalThis.testFiles = (globalThis.testFiles ?? 0) + 1 + + test('setup file ran before this file', () => { + expect(setupRuns).toBe(globalThis.testFiles) + }) + ` + const related = await runInlineTests( + { + 'vitest.config.js': { + test: { + isolate: false, + setupFiles: ['./setup.js'], + fsModuleCache: true, + fsModuleCachePath: './node_modules/.vitest-fs-cache', + }, + }, + 'setup.js': `globalThis.setupRuns = (globalThis.setupRuns ?? 0) + 1`, + 'dep.js': `export const value = 1`, + 'a.test.js': testFile, + 'b.test.js': testFile, + }, + { related: ['dep.js'] }, + ) + const cached = await runVitest({ root: related.root }) + + for (const run of [related, cached]) { + expect(run.stderr).toBe('') + expect(run.errorTree()).toMatchInlineSnapshot(` + { + "a.test.js": { + "setup file ran before this file": "passed", + }, + "b.test.js": { + "setup file ran before this file": "passed", + }, + } + `) + } +}) + test('if cache key generator bails out, the file is not cached', async () => { process.env.REPLACED = 'value1'