···6060 `)
6161})
62626363+test('recognizes the vite-plus/test redistribution as the hoisted module', () => {
6464+ expect(hoistSimpleCode(`
6565+import { vi } from 'vite-plus/test'
6666+vi.mock('path', () => {})
6767+vi.unmock('path')
6868+vi.hoisted(() => {})
6969+ `)).toMatchInlineSnapshot(`
7070+ "vi.mock('path', () => {})
7171+ vi.unmock('path')
7272+ vi.hoisted(() => {})
7373+7474+ import { vi } from 'vite-plus/test'"
7575+ `)
7676+})
7777+6378test('always hoists all imports but they are under mocks', () => {
6479 expect(hoistSimpleCode(`
6580 import { vi } from 'vitest'
+9-2
packages/mocker/src/node/hoistMocks.ts
···8383 = /\b(?:vi|vitest)\s*\.\s*(?:mock|unmock|hoisted|doMock|doUnmock)\s*\(/
8484const hashbangRE = /^#!.*\n/
85858686+// Public redistributions of Vitest that re-export its mocking API (`vi`)
8787+// verbatim under their own specifier. Imports from these are treated as the
8888+// hoisted module so `vi.mock()` is hoisted for e.g.
8989+// `import { vi } from 'vite-plus/test'`, exactly as it is for `vitest`.
9090+const REDISTRIBUTED_HOISTED_MODULES = ['vite-plus/test']
9191+8692// this is a fork of Vite SSR transform
8793export function hoistMocks(
8894 code: string,
···141147 ) {
142148 const source = importNode.source.value as string
143149 // always hoist vitest import to top of the file, so
144144- // "vi" helpers can access it
145145- if (hoistedModule === source) {
150150+ // "vi" helpers can access it. Vitest redistributions that re-export the
151151+ // mocking API under their own specifier are recognized the same way.
152152+ if (hoistedModule === source || REDISTRIBUTED_HOISTED_MODULES.includes(source)) {
146153 hoistedModuleImported = true
147154 return
148155 }