From 0f9815112264b544312257e06dc2f2495d8cf928 Mon Sep 17 00:00:00 2001 From: Blake Newman Date: Thu, 11 Jan 2024 12:26:03 +0000 Subject: [PATCH] fix(vitest): test deep dependencies change detection `--changed` flag is not correctly looking up deep dependencies, as such the changed flag does not trigger specs if a sub dependency is changed. Alters the logic to add the collected dependency after recursive `addImports` is called, otherwise the logic returns early because that dependency has already been added to the collected deps set. So it will not look deeply. --- packages/vitest/src/node/core.ts | 8 ++++---- .../fixtures/related/deep-related-exports.test.ts | 11 +++++++++++ .../fixtures/related/deep-related-imports.test.ts | 11 +++++++++++ test/changed/fixtures/related/src/sourceC.ts | 2 ++ test/changed/fixtures/related/src/sourceD.ts | 4 ++++ test/changed/tests/related.test.ts | 2 ++ 6 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/changed/fixtures/related/deep-related-exports.test.ts create mode 100644 test/changed/fixtures/related/deep-related-imports.test.ts create mode 100644 test/changed/fixtures/related/src/sourceC.ts create mode 100644 test/changed/fixtures/related/src/sourceD.ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 6f90f2df7bf88..d12b5e3f665c9 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -389,6 +389,8 @@ export class Vitest { const addImports = async ([project, filepath]: WorkspaceSpec) => { if (deps.has(filepath)) return + deps.add(filepath) + const mod = project.server.moduleGraph.getModuleById(filepath) const transformed = mod?.ssrTransformResult || await project.vitenode.transformRequest(filepath) if (!transformed) @@ -397,15 +399,13 @@ export class Vitest { await Promise.all(dependencies.map(async (dep) => { const path = await project.server.pluginContainer.resolveId(dep, filepath, { ssr: true }) const fsPath = path && !path.external && path.id.split('?')[0] - if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath)) { - deps.add(fsPath) - + if (fsPath && !fsPath.includes('node_modules') && !deps.has(fsPath) && existsSync(fsPath)) await addImports([project, fsPath]) - } })) } await addImports(filepath) + deps.delete(filepath[1]) return deps } diff --git a/test/changed/fixtures/related/deep-related-exports.test.ts b/test/changed/fixtures/related/deep-related-exports.test.ts new file mode 100644 index 0000000000000..a9e729d4458b2 --- /dev/null +++ b/test/changed/fixtures/related/deep-related-exports.test.ts @@ -0,0 +1,11 @@ +import { access } from 'node:fs' +import { sep } from 'pathe' +import { expect, test } from 'vitest' +import { A } from './src/sourceC' + +test('values', () => { + expect(A).toBe('A') + expect(typeof sep).toBe('string') + // doesn't throw + expect(typeof access).toBe('function') +}) diff --git a/test/changed/fixtures/related/deep-related-imports.test.ts b/test/changed/fixtures/related/deep-related-imports.test.ts new file mode 100644 index 0000000000000..29755dd1bdea0 --- /dev/null +++ b/test/changed/fixtures/related/deep-related-imports.test.ts @@ -0,0 +1,11 @@ +import { access } from 'node:fs' +import { sep } from 'pathe' +import { expect, test } from 'vitest' +import { A } from './src/sourceD' + +test('values', () => { + expect(A).toBe('A') + expect(typeof sep).toBe('string') + // doesn't throw + expect(typeof access).toBe('function') +}) diff --git a/test/changed/fixtures/related/src/sourceC.ts b/test/changed/fixtures/related/src/sourceC.ts new file mode 100644 index 0000000000000..f4ab565795744 --- /dev/null +++ b/test/changed/fixtures/related/src/sourceC.ts @@ -0,0 +1,2 @@ +// re-exporting for deep changed detection +export { A } from './sourceA' diff --git a/test/changed/fixtures/related/src/sourceD.ts b/test/changed/fixtures/related/src/sourceD.ts new file mode 100644 index 0000000000000..2b8d12d4bd754 --- /dev/null +++ b/test/changed/fixtures/related/src/sourceD.ts @@ -0,0 +1,4 @@ +// import and re-exporting for deep changed detection +import { A as sourceA } from './sourceA' + +export const A = sourceA diff --git a/test/changed/tests/related.test.ts b/test/changed/tests/related.test.ts index 5cf2ac66f9724..239756a3685da 100644 --- a/test/changed/tests/related.test.ts +++ b/test/changed/tests/related.test.ts @@ -8,5 +8,7 @@ it('related correctly runs only related tests', async () => { expect(stderr).toBe('') expect(stdout).toContain('1 passed') expect(stdout).toContain('related.test.ts') + expect(stdout).toContain('deep-related-imports.test.ts') + expect(stdout).toContain('deep-related-exports.test.ts') expect(stdout).not.toContain('not-related.test.ts') })