File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -74,6 +74,12 @@ const regexpHoistable
7474 = / \b (?: v i | v i t e s t ) \s * \. \s * (?: m o c k | u n m o c k | h o i s t e d | d o M o c k | d o U n m o c k ) \s * \( /
7575const hashbangRE = / ^ # ! .* \n /
7676
77+ // Public redistributions of Vitest that re-export its mocking API (`vi`)
78+ // verbatim under their own specifier. Imports from these are treated as the
79+ // hoisted module so `vi.mock()` is hoisted for e.g.
80+ // `import { vi } from 'vite-plus/test'`, exactly as it is for `vitest`.
81+ const REDISTRIBUTED_HOISTED_MODULES = [ 'vite-plus/test' ]
82+
7783// this is a fork of Vite SSR transform
7884export function hoistMocks (
7985 code : string ,
@@ -132,8 +138,9 @@ export function hoistMocks(
132138 ) {
133139 const source = importNode . source . value as string
134140 // always hoist vitest import to top of the file, so
135- // "vi" helpers can access it
136- if ( hoistedModule === source ) {
141+ // "vi" helpers can access it. Vitest redistributions that re-export the
142+ // mocking API under their own specifier are recognized the same way.
143+ if ( hoistedModule === source || REDISTRIBUTED_HOISTED_MODULES . includes ( source ) ) {
137144 hoistedModuleImported = true
138145 return
139146 }
Original file line number Diff line number Diff line change @@ -57,6 +57,21 @@ import { test } from 'vitest'
5757 ` )
5858} )
5959
60+ test ( 'recognizes the vite-plus/test redistribution as the hoisted module' , ( ) => {
61+ expect ( hoistSimpleCode ( `
62+ import { vi } from 'vite-plus/test'
63+ vi.mock('path', () => {})
64+ vi.unmock('path')
65+ vi.hoisted(() => {})
66+ ` ) ) . toMatchInlineSnapshot ( `
67+ "vi.mock('path', () => {})
68+ vi.unmock('path')
69+ vi.hoisted(() => {})
70+
71+ import { vi } from 'vite-plus/test'"
72+ ` )
73+ } )
74+
6075test ( 'always hoists all imports but they are under mocks' , ( ) => {
6176 expect ( hoistSimpleCode ( `
6277 import { vi } from 'vitest'
You can’t perform that action at this time.
0 commit comments