Skip to content
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
11 changes: 9 additions & 2 deletions packages/mocker/src/node/hoistMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ const regexpHoistable
= /\b(?:vi|vitest)\s*\.\s*(?:mock|unmock|hoisted|doMock|doUnmock)\s*\(/
const hashbangRE = /^#!.*\n/

// Public redistributions of Vitest that re-export its mocking API (`vi`)
// verbatim under their own specifier. Imports from these are treated as the
// hoisted module so `vi.mock()` is hoisted for e.g.
// `import { vi } from 'vite-plus/test'`, exactly as it is for `vitest`.
const REDISTRIBUTED_HOISTED_MODULES = ['vite-plus/test']

// this is a fork of Vite SSR transform
export function hoistMocks(
code: string,
Expand Down Expand Up @@ -132,8 +138,9 @@ export function hoistMocks(
) {
const source = importNode.source.value as string
// always hoist vitest import to top of the file, so
// "vi" helpers can access it
if (hoistedModule === source) {
// "vi" helpers can access it. Vitest redistributions that re-export the
// mocking API under their own specifier are recognized the same way.
if (hoistedModule === source || REDISTRIBUTED_HOISTED_MODULES.includes(source)) {
hoistedModuleImported = true
return
}
Expand Down
15 changes: 15 additions & 0 deletions test/core/test/injector-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ import { test } from 'vitest'
`)
})

test('recognizes the vite-plus/test redistribution as the hoisted module', () => {
expect(hoistSimpleCode(`
import { vi } from 'vite-plus/test'
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})
`)).toMatchInlineSnapshot(`
"vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})

import { vi } from 'vite-plus/test'"
`)
})

test('always hoists all imports but they are under mocks', () => {
expect(hoistSimpleCode(`
import { vi } from 'vitest'
Expand Down
Loading