Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): test deep dependencies change detection #4934

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions test/changed/fixtures/related/deep-related-exports.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
11 changes: 11 additions & 0 deletions test/changed/fixtures/related/deep-related-imports.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
2 changes: 2 additions & 0 deletions test/changed/fixtures/related/src/sourceC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// re-exporting for deep changed detection
export { A } from './sourceA'
4 changes: 4 additions & 0 deletions test/changed/fixtures/related/src/sourceD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// import and re-exporting for deep changed detection
import { A as sourceA } from './sourceA'

export const A = sourceA
4 changes: 3 additions & 1 deletion test/changed/tests/related.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { runVitestCli } from '../../test-utils'
it('related correctly runs only related tests', async () => {
const { stdout, stderr } = await runVitestCli('related', join(process.cwd(), 'fixtures/related/src/sourceA.ts'), '--root', join(process.cwd(), './fixtures/related'), '--globals', '--no-watch')
expect(stderr).toBe('')
expect(stdout).toContain('1 passed')
expect(stdout).toContain('3 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')
})
Loading