Skip to content

Commit dcf2e9f

Browse files
authored
fix(vitest): handle single await vi.hoisted (#4962)
1 parent b561c32 commit dcf2e9f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/vitest/src/node/hoistMocks.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ export function getBetterEnd(code: string, node: Node) {
5959
return end
6060
}
6161

62-
const regexpHoistable = /^[ \t]*\b(vi|vitest)\s*\.\s*(mock|unmock|hoisted)\(/m
63-
const regexpAssignedHoisted = /=[ \t]*(\bawait|)[ \t]*\b(vi|vitest)\s*\.\s*hoisted\(/
62+
const regexpHoistable = /[ \t]*\b(vi|vitest)\s*\.\s*(mock|unmock|hoisted)\(/
6463
const hashbangRE = /^#!.*\n/
6564

6665
export function hoistMocks(code: string, id: string, parse: PluginContext['parse']) {
67-
const needHoisting = regexpHoistable.test(code) || regexpAssignedHoisted.test(code)
66+
const needHoisting = regexpHoistable.test(code)
6867

6968
if (!needHoisting)
7069
return

test/core/test/injector-mock.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,24 @@ console.log(foo + 2)
11821182
console.log(__vi_import_0__.foo + 2)"
11831183
`)
11841184
})
1185+
1186+
test('handle single "await vi.hoisted"', async () => {
1187+
expect(
1188+
hoistSimpleCode(`
1189+
import { vi } from 'vitest';
1190+
1234;
1191+
await vi
1192+
.hoisted(() => {});
1193+
`),
1194+
).toMatchInlineSnapshot(`
1195+
"const { vi } = await import('vitest')
1196+
await vi
1197+
.hoisted(() => {});
1198+
1199+
1200+
1234;"
1201+
`)
1202+
})
11851203
})
11861204

11871205
describe('throws an error when nodes are incompatible', () => {

0 commit comments

Comments
 (0)