[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(mocker): hoist vi.mock() for vite-plus/test imports (#10489)

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>

authored by

LongYinan
Claude Opus 4.8
and committed by
GitHub
(Jun 1, 2026, 12:12 PM +0200) 8837664a b9ee71e6

+24 -2
+15
test/unit/test/injector-mock.test.ts
··· 60 60 `) 61 61 }) 62 62 63 + test('recognizes the vite-plus/test redistribution as the hoisted module', () => { 64 + expect(hoistSimpleCode(` 65 + import { vi } from 'vite-plus/test' 66 + vi.mock('path', () => {}) 67 + vi.unmock('path') 68 + vi.hoisted(() => {}) 69 + `)).toMatchInlineSnapshot(` 70 + "vi.mock('path', () => {}) 71 + vi.unmock('path') 72 + vi.hoisted(() => {}) 73 + 74 + import { vi } from 'vite-plus/test'" 75 + `) 76 + }) 77 + 63 78 test('always hoists all imports but they are under mocks', () => { 64 79 expect(hoistSimpleCode(` 65 80 import { vi } from 'vitest'
+9 -2
packages/mocker/src/node/hoistMocks.ts
··· 83 83 = /\b(?:vi|vitest)\s*\.\s*(?:mock|unmock|hoisted|doMock|doUnmock)\s*\(/ 84 84 const hashbangRE = /^#!.*\n/ 85 85 86 + // Public redistributions of Vitest that re-export its mocking API (`vi`) 87 + // verbatim under their own specifier. Imports from these are treated as the 88 + // hoisted module so `vi.mock()` is hoisted for e.g. 89 + // `import { vi } from 'vite-plus/test'`, exactly as it is for `vitest`. 90 + const REDISTRIBUTED_HOISTED_MODULES = ['vite-plus/test'] 91 + 86 92 // this is a fork of Vite SSR transform 87 93 export function hoistMocks( 88 94 code: string, ··· 141 147 ) { 142 148 const source = importNode.source.value as string 143 149 // always hoist vitest import to top of the file, so 144 - // "vi" helpers can access it 145 - if (hoistedModule === source) { 150 + // "vi" helpers can access it. Vitest redistributions that re-export the 151 + // mocking API under their own specifier are recognized the same way. 152 + if (hoistedModule === source || REDISTRIBUTED_HOISTED_MODULES.includes(source)) { 146 153 hoistedModuleImported = true 147 154 return 148 155 }