[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(mock): inject helpers after hashbang if present (#9545)

authored by

Vladimir and committed by
GitHub
(Jan 28, 2026, 5:34 PM +0100) 65432a74 5b16483c

+43 -7
+38 -4
test/core/test/injector-mock.test.ts
··· 915 915 // #8002 916 916 test('with hashbang', () => { 917 917 expect( 918 - hoistSimpleCodeWithoutMocks( 918 + hoistSimpleCode( 919 919 `#!/usr/bin/env node 920 + vi.mock('foo'); 920 921 console.log("it can parse the hashbang")`, 921 922 ), 922 - ).toMatchInlineSnapshot(`undefined`) 923 + ).toMatchInlineSnapshot(` 924 + "#!/usr/bin/env node 925 + import { vi } from "vitest" 926 + vi.mock('foo'); 927 + console.log("it can parse the hashbang")" 928 + `) 923 929 }) 924 930 925 931 test('import hoisted after hashbang', () => { 926 932 expect( 927 - hoistSimpleCodeWithoutMocks( 933 + hoistSimpleCode( 928 934 `#!/usr/bin/env node 935 + vi.mock('foo'); 929 936 console.log(foo); 930 937 import foo from "foo"`, 931 938 ), 932 - ).toMatchInlineSnapshot(`undefined`) 939 + ).toMatchInlineSnapshot(` 940 + "#!/usr/bin/env node 941 + import { vi } from "vitest" 942 + vi.mock('foo'); 943 + const __vi_import_0__ = await import("foo"); 944 + console.log(__vi_import_0__.default);" 945 + `) 946 + }) 947 + 948 + test('import hoisted after hashbang', () => { 949 + expect( 950 + hoistSimpleCode( 951 + `#!/usr/bin/env node 952 + import { vi } from './proxy' 953 + vi.mock('foo'); 954 + console.log(foo); 955 + import foo from "foo"`, 956 + ), 957 + ).toMatchInlineSnapshot(` 958 + "#!/usr/bin/env node 959 + 960 + if (typeof globalThis["vi"] === "undefined") { throw new Error("There are some problems in resolving the mocks API.\\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'.\\nTo fix this issue you can either:\\n- import the mocks API directly from 'vitest'\\n- enable the 'globals' option") } 961 + __vi_import_0__.vi.mock('foo'); 962 + const __vi_import_0__ = await import("./proxy"); 963 + const __vi_import_1__ = await import("foo"); 964 + 965 + console.log(__vi_import_1__.default);" 966 + `) 933 967 }) 934 968 935 969 // #10289
+5 -3
packages/mocker/src/node/hoistMocks.ts
··· 107 107 } = options 108 108 109 109 // hoist at the start of the file, after the hashbang 110 - let hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0 110 + const hashbangEnd = hashbangRE.exec(code)?.[0].length ?? 0 111 + let hoistIndex = hashbangEnd 111 112 112 113 let hoistedModuleImported = false 113 114 ··· 535 536 const utilityImports = [...usedUtilityExports] 536 537 // "vi" or "vitest" is imported from a module other than "vitest" 537 538 if (utilityImports.some(name => idToImportMap.has(name))) { 538 - s.prepend(API_NOT_FOUND_CHECK(utilityImports)) 539 + s.appendLeft(hashbangEnd, API_NOT_FOUND_CHECK(utilityImports)) 539 540 } 540 541 // if "vi" or "vitest" are not imported at all, import them 541 542 else if (utilityImports.length) { 542 - s.prepend( 543 + s.appendLeft( 544 + hashbangEnd, 543 545 `import { ${[...usedUtilityExports].join(', ')} } from ${JSON.stringify( 544 546 hoistedModule, 545 547 )}\n`,