Skip to content

Commit 9c7c0fc

Browse files
authoredJan 20, 2024
fix(vitest): test deep dependencies change detection (#4934)
1 parent d53b858 commit 9c7c0fc

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed
 

‎packages/vitest/src/node/core.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ export class Vitest {
390390
const addImports = async ([project, filepath]: WorkspaceSpec) => {
391391
if (deps.has(filepath))
392392
return
393+
deps.add(filepath)
394+
393395
const mod = project.server.moduleGraph.getModuleById(filepath)
394396
const transformed = mod?.ssrTransformResult || await project.vitenode.transformRequest(filepath)
395397
if (!transformed)
@@ -398,15 +400,13 @@ export class Vitest {
398400
await Promise.all(dependencies.map(async (dep) => {
399401
const path = await project.server.pluginContainer.resolveId(dep, filepath, { ssr: true })
400402
const fsPath = path && !path.external && path.id.split('?')[0]
401-
if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath)) {
402-
deps.add(fsPath)
403-
403+
if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath))
404404
await addImports([project, fsPath])
405-
}
406405
}))
407406
}
408407

409408
await addImports(filepath)
409+
deps.delete(filepath[1])
410410

411411
return deps
412412
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { access } from 'node:fs'
2+
import { sep } from 'pathe'
3+
import { expect, test } from 'vitest'
4+
import { A } from './src/sourceC'
5+
6+
test('values', () => {
7+
expect(A).toBe('A')
8+
expect(typeof sep).toBe('string')
9+
// doesn't throw
10+
expect(typeof access).toBe('function')
11+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { access } from 'node:fs'
2+
import { sep } from 'pathe'
3+
import { expect, test } from 'vitest'
4+
import { A } from './src/sourceD'
5+
6+
test('values', () => {
7+
expect(A).toBe('A')
8+
expect(typeof sep).toBe('string')
9+
// doesn't throw
10+
expect(typeof access).toBe('function')
11+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// re-exporting for deep changed detection
2+
export { A } from './sourceA'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// import and re-exporting for deep changed detection
2+
import { A as sourceA } from './sourceA'
3+
4+
export const A = sourceA

‎test/changed/tests/related.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { runVitestCli } from '../../test-utils'
66
it('related correctly runs only related tests', async () => {
77
const { stdout, stderr } = await runVitestCli('related', join(process.cwd(), 'fixtures/related/src/sourceA.ts'), '--root', join(process.cwd(), './fixtures/related'), '--globals', '--no-watch')
88
expect(stderr).toBe('')
9-
expect(stdout).toContain('1 passed')
9+
expect(stdout).toContain('3 passed')
1010
expect(stdout).toContain('related.test.ts')
11+
expect(stdout).toContain('deep-related-imports.test.ts')
12+
expect(stdout).toContain('deep-related-exports.test.ts')
1113
expect(stdout).not.toContain('not-related.test.ts')
1214
})

0 commit comments

Comments
 (0)
Please sign in to comment.