Skip to content

Commit 3a39cfc

Browse files
authored
fix: rerun setup files after --changed filtering (#11621)
1 parent 7a825db commit 3a39cfc

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎packages/vitest/src/node/cache/fsModuleCache.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export class FileSystemModuleCache {
146146

147147
return {
148148
id: meta.id,
149-
url: meta.url,
150149
file: meta.file,
151150
code,
152151
imports: meta.imports,
@@ -169,7 +168,6 @@ export class FileSystemModuleCache {
169168
const result = {
170169
file: fetchResult.file,
171170
id: fetchResult.id,
172-
url: fetchResult.url,
173171
imports,
174172
mappings,
175173
moduleType: fetchResult.moduleType,
@@ -411,7 +409,6 @@ export async function atomicWriteFile(realFilePath: string, data: string): Promi
411409
}
412410

413411
export interface CachedInlineModuleMeta {
414-
url: string
415412
id: string
416413
file: string | null
417414
code: string

‎packages/vitest/src/node/environments/transformService.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ export class ModuleTransformService {
8383
*/
8484
async transform(id: string, environment: DevEnvironment): Promise<TransformResult | null> {
8585
await this.traces.waitInit()
86+
// the module graph keeps the url of the first request, so it has to be the url that workers import
87+
const url = normalizeResolvedIdToUrl(environment, id)
8688
const result = await this.traces.$('vitest.module.transform', (span) =>
87-
this.fetchInSpan(span, id, undefined, environment),
89+
this.fetchInSpan(span, url, undefined, environment),
8890
)
8991
// externalized modules are loaded by Node without a transform, like in a normal run
9092
if ('externalize' in result) {
@@ -96,7 +98,7 @@ export class ModuleTransformService {
9698
return transformResult
9799
}
98100
}
99-
return environment.transformRequest(id)
101+
return environment.transformRequest(url)
100102
}
101103

102104
private async fetchInSpan(
@@ -400,7 +402,7 @@ export class ModuleTransformService {
400402
file: cachedModule.file,
401403
id: cachedModule.id,
402404
tmp: cachePath,
403-
url: cachedModule.url,
405+
url: moduleGraphModule.url,
404406
invalidate: false,
405407
moduleType,
406408
}

‎test/e2e/test/caching.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,52 @@ test('filtering by related sources reuses the cache', async () => {
267267
delete (globalThis as any).__vitest_transformed__
268268
})
269269

270+
test('filtering by related sources runs setup files before every non-isolated file', async () => {
271+
const testFile = `
272+
import { expect, test } from 'vitest'
273+
import './dep.js'
274+
275+
const setupRuns = globalThis.setupRuns
276+
globalThis.testFiles = (globalThis.testFiles ?? 0) + 1
277+
278+
test('setup file ran before this file', () => {
279+
expect(setupRuns).toBe(globalThis.testFiles)
280+
})
281+
`
282+
const related = await runInlineTests(
283+
{
284+
'vitest.config.js': {
285+
test: {
286+
isolate: false,
287+
setupFiles: ['./setup.js'],
288+
fsModuleCache: true,
289+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
290+
},
291+
},
292+
'setup.js': `globalThis.setupRuns = (globalThis.setupRuns ?? 0) + 1`,
293+
'dep.js': `export const value = 1`,
294+
'a.test.js': testFile,
295+
'b.test.js': testFile,
296+
},
297+
{ related: ['dep.js'] },
298+
)
299+
const cached = await runVitest({ root: related.root })
300+
301+
for (const run of [related, cached]) {
302+
expect(run.stderr).toBe('')
303+
expect(run.errorTree()).toMatchInlineSnapshot(`
304+
{
305+
"a.test.js": {
306+
"setup file ran before this file": "passed",
307+
},
308+
"b.test.js": {
309+
"setup file ran before this file": "passed",
310+
},
311+
}
312+
`)
313+
}
314+
})
315+
270316
test('if cache key generator bails out, the file is not cached', async () => {
271317
process.env.REPLACED = 'value1'
272318

0 commit comments

Comments
 (0)