Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 325463a

Browse filesBrowse files
authored
fix(ast-collect): recognize __vi_import_ prefix in static test discovery (#10129)
1 parent e7bc154 commit 325463a
Copy full SHA for 325463a

2 files changed

+94Lines changed: 94 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/vitest/src/node/ast-collect.ts‎

Copy file name to clipboardExpand all lines: packages/vitest/src/node/ast-collect.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ function astParseFile(filepath: string, code: string) {
9696
if (
9797
// direct call as `__vite_ssr_exports_0__.test()`
9898
callee.object?.name?.startsWith('__vite_ssr_')
99+
// Vitest's module mocker uses `__vi_import_N__` for mocked/dynamic imports
100+
// e.g. `__vi_import_0__.it()` when vi.mock is present in the file
101+
|| callee.object?.name?.startsWith('__vi_import_')
99102
// call as `__vite_ssr_exports_0__.Vitest.test`,
100103
// this is a special case for using Vitest namespaces popular in Effect
101104
|| (callee.object?.object?.name?.startsWith('__vite_ssr_') && callee.object?.property?.name === 'Vitest')
Collapse file

‎test/cli/test/static-collect.test.ts‎

Copy file name to clipboardExpand all lines: test/cli/test/static-collect.test.ts
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,97 @@ test('collects tests imported from another file', async () => {
427427
`)
428428
})
429429

430+
test('collects tests imported from another file while a vi.mock line is present', async () => {
431+
const testModule = await collectTests(`
432+
import { describe } from 'vitest';
433+
import { it } from './Utils/test-extend.ts';
434+
435+
vi.mock('@/composables/test.js', async (importOriginal) => { });
436+
437+
describe('should included', () => {
438+
it('is included', () => {});
439+
});
440+
441+
describe('top level describe', () => {
442+
describe('nested describe', () => {
443+
it('is included', ({ server }) => {});
444+
});
445+
});
446+
`)
447+
expect(testModule).toMatchInlineSnapshot(`
448+
{
449+
"should included": {
450+
"is included": {
451+
"errors": [],
452+
"fullName": "should included > is included",
453+
"id": "-1732721377_0_0",
454+
"location": "8:6",
455+
"mode": "run",
456+
"state": "pending",
457+
},
458+
},
459+
"top level describe": {
460+
"nested describe": {
461+
"is included": {
462+
"errors": [],
463+
"fullName": "top level describe > nested describe > is included",
464+
"id": "-1732721377_1_0_0",
465+
"location": "13:8",
466+
"mode": "run",
467+
"state": "pending",
468+
},
469+
},
470+
},
471+
}
472+
`)
473+
})
474+
475+
test('collects tests imported from another file while a vi.mock line is present and commented', async () => {
476+
const testModule = await collectTests(`
477+
import { describe } from 'vitest';
478+
import { it } from './Utils/test-extend.ts';
479+
480+
// vi.mock('@/composables/test.js', async (importOriginal) => { });
481+
482+
describe('should included', () => {
483+
it('is included', () => {});
484+
});
485+
486+
describe('top level describe', () => {
487+
describe('nested describe', () => {
488+
it('is included', ({ server }) => {});
489+
});
490+
});
491+
`)
492+
493+
expect(testModule).toMatchInlineSnapshot(`
494+
{
495+
"should included": {
496+
"is included": {
497+
"errors": [],
498+
"fullName": "should included > is included",
499+
"id": "-1732721377_0_0",
500+
"location": "8:6",
501+
"mode": "run",
502+
"state": "pending",
503+
},
504+
},
505+
"top level describe": {
506+
"nested describe": {
507+
"is included": {
508+
"errors": [],
509+
"fullName": "top level describe > nested describe > is included",
510+
"id": "-1732721377_1_0_0",
511+
"location": "13:8",
512+
"mode": "run",
513+
"state": "pending",
514+
},
515+
},
516+
},
517+
}
518+
`)
519+
})
520+
430521
test('collects mixed test function names', async () => {
431522
const testModule = await collectTests(`
432523
import { it, test, testUnit, integrationTest } from 'vitest'

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.