Skip to content

Commit

Permalink
fix(vitest): handle single await vi.hoisted (#4962)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 15, 2024
1 parent b561c32 commit dcf2e9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/vitest/src/node/hoistMocks.ts
Expand Up @@ -59,12 +59,11 @@ export function getBetterEnd(code: string, node: Node) {
return end
}

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

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

if (!needHoisting)
return
Expand Down
18 changes: 18 additions & 0 deletions test/core/test/injector-mock.test.ts
Expand Up @@ -1182,6 +1182,24 @@ console.log(foo + 2)
console.log(__vi_import_0__.foo + 2)"
`)
})

test('handle single "await vi.hoisted"', async () => {
expect(
hoistSimpleCode(`
import { vi } from 'vitest';
1234;
await vi
.hoisted(() => {});
`),
).toMatchInlineSnapshot(`
"const { vi } = await import('vitest')
await vi
.hoisted(() => {});
1234;"
`)
})
})

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

0 comments on commit dcf2e9f

Please sign in to comment.