Skip to content

Commit 4a28a9d

Browse files
authored
test: add test case for mocking invalid package and module (#9617)
1 parent 84b5c76 commit 4a28a9d

File tree

8 files changed

+456
-175
lines changed

8 files changed

+456
-175
lines changed

pnpm-lock.yaml

Lines changed: 301 additions & 174 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "test-dep-invalid",
3+
"private": true
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-expect-error no type
2+
import * as dep from 'test-dep-invalid'
3+
import { expect, test, vi } from 'vitest'
4+
5+
vi.mock('test-dep-invalid', () => ({ mocked: 'ok' }))
6+
7+
test('basic', () => {
8+
expect(dep).toMatchObject({ mocked: 'ok' })
9+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { hello } from './wrapper.js'
3+
4+
vi.mock('test-dep-invalid', () => ({}))
5+
6+
vi.mock(import('./wrapper.js'), () => {
7+
return { hello: () => 'mock-hello' }
8+
})
9+
10+
test('basic', () => {
11+
expect(hello()).toBe('mock-hello')
12+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { hello } from './wrapper.js'
3+
4+
vi.mock(import('./wrapper.js'), () => {
5+
return { hello: () => 'mock-hello' }
6+
})
7+
8+
test('basic', () => {
9+
expect(hello()).toBe('mock-hello')
10+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @ts-expect-error no type
2+
import * as dep from 'test-dep-invalid'
3+
4+
export const hello = () => dep

test/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"flatted": "catalog:",
3030
"obug": "^2.1.1",
3131
"playwright": "catalog:",
32+
"test-dep-invalid": "link:./deps/dep-invalid",
3233
"tinyspy": "catalog:",
3334
"typescript": "catalog:",
3435
"unplugin-swc": "^1.5.9",

test/cli/test/mocking.test.ts

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import path from 'node:path'
12
import { expect, test } from 'vitest'
2-
import { runInlineTests } from '../../test-utils'
3+
import { rolldownVersion } from 'vitest/node'
4+
import { runInlineTests, runVitest } from '../../test-utils'
35

46
test('setting resetMocks works if restoreMocks is also set', async () => {
57
const { stderr, testTree } = await runInlineTests({
@@ -39,3 +41,115 @@ test('spy is not called here', () => {
3941
}
4042
`)
4143
})
44+
45+
test('invalid packages', async () => {
46+
const { results, errorTree } = await runVitest({
47+
root: path.join(import.meta.dirname, '../fixtures/invalid-package'),
48+
})
49+
const testModuleErrors = Object.fromEntries(
50+
results.map(testModule => [
51+
testModule.relativeModuleId,
52+
testModule.errors().map(e => e.message),
53+
]),
54+
)
55+
56+
// requires Vite 8 for relaxed import analysis validataion
57+
// https://github.com/vitejs/vite/pull/21601
58+
if (rolldownVersion) {
59+
expect(testModuleErrors).toMatchInlineSnapshot(`
60+
{
61+
"mock-bad-dep.test.ts": [],
62+
"mock-wrapper-and-bad-dep.test.ts": [],
63+
"mock-wrapper.test.ts": [],
64+
}
65+
`)
66+
expect(errorTree()).toMatchInlineSnapshot(`
67+
{
68+
"mock-bad-dep.test.ts": {
69+
"basic": "passed",
70+
},
71+
"mock-wrapper-and-bad-dep.test.ts": {
72+
"basic": "passed",
73+
},
74+
"mock-wrapper.test.ts": {
75+
"basic": "passed",
76+
},
77+
}
78+
`)
79+
}
80+
else {
81+
expect(testModuleErrors).toMatchInlineSnapshot(`
82+
{
83+
"mock-bad-dep.test.ts": [
84+
"Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.",
85+
],
86+
"mock-wrapper-and-bad-dep.test.ts": [
87+
"Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.",
88+
],
89+
"mock-wrapper.test.ts": [
90+
"Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.",
91+
],
92+
}
93+
`)
94+
expect(errorTree()).toMatchInlineSnapshot(`
95+
{
96+
"mock-bad-dep.test.ts": {},
97+
"mock-wrapper-and-bad-dep.test.ts": {},
98+
"mock-wrapper.test.ts": {},
99+
}
100+
`)
101+
}
102+
})
103+
104+
test('mocking modules with syntax error', async () => {
105+
// TODO: manual mocked module still gets transformed so this is not supported yet.
106+
const { errorTree, results } = await runInlineTests({
107+
'./syntax-error.js': `syntax error`,
108+
'./basic.test.js': /* ts */ `
109+
import * as dep from './syntax-error.js'
110+
111+
vi.mock('./syntax-error.js', () => {
112+
return { mocked: 'ok' }
113+
})
114+
115+
test('can mock invalid module', () => {
116+
expect(dep).toMatchObject({ mocked: 'ok' })
117+
})
118+
`,
119+
})
120+
121+
const testModuleErrors = Object.fromEntries(
122+
results.map(testModule => [
123+
testModule.relativeModuleId,
124+
testModule.errors().map(e => e.message),
125+
]),
126+
)
127+
if (rolldownVersion) {
128+
expect(testModuleErrors).toMatchInlineSnapshot(`
129+
{
130+
"basic.test.js": [
131+
"Parse failure: Parse failed with 1 error:
132+
Expected a semicolon or an implicit semicolon after a statement, but found none
133+
1: syntax error
134+
^
135+
At file: /syntax-error.js:1:6",
136+
],
137+
}
138+
`)
139+
}
140+
else {
141+
expect(testModuleErrors).toMatchInlineSnapshot(`
142+
{
143+
"basic.test.js": [
144+
"Parse failure: Expected ';', '}' or <eof>
145+
At file: /syntax-error.js:1:7",
146+
],
147+
}
148+
`)
149+
}
150+
expect(errorTree()).toMatchInlineSnapshot(`
151+
{
152+
"basic.test.js": {},
153+
}
154+
`)
155+
})

0 commit comments

Comments
 (0)