···915915 // #8002
916916 test('with hashbang', () => {
917917 expect(
918918- hoistSimpleCodeWithoutMocks(
918918+ hoistSimpleCode(
919919 `#!/usr/bin/env node
920920+vi.mock('foo');
920921console.log("it can parse the hashbang")`,
921922 ),
922922- ).toMatchInlineSnapshot(`undefined`)
923923+ ).toMatchInlineSnapshot(`
924924+ "#!/usr/bin/env node
925925+ import { vi } from "vitest"
926926+ vi.mock('foo');
927927+ console.log("it can parse the hashbang")"
928928+ `)
923929 })
924930925931 test('import hoisted after hashbang', () => {
926932 expect(
927927- hoistSimpleCodeWithoutMocks(
933933+ hoistSimpleCode(
928934 `#!/usr/bin/env node
935935+vi.mock('foo');
929936console.log(foo);
930937import foo from "foo"`,
931938 ),
932932- ).toMatchInlineSnapshot(`undefined`)
939939+ ).toMatchInlineSnapshot(`
940940+ "#!/usr/bin/env node
941941+ import { vi } from "vitest"
942942+ vi.mock('foo');
943943+ const __vi_import_0__ = await import("foo");
944944+ console.log(__vi_import_0__.default);"
945945+ `)
946946+ })
947947+948948+ test('import hoisted after hashbang', () => {
949949+ expect(
950950+ hoistSimpleCode(
951951+ `#!/usr/bin/env node
952952+import { vi } from './proxy'
953953+vi.mock('foo');
954954+console.log(foo);
955955+import foo from "foo"`,
956956+ ),
957957+ ).toMatchInlineSnapshot(`
958958+ "#!/usr/bin/env node
959959+960960+ 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") }
961961+ __vi_import_0__.vi.mock('foo');
962962+ const __vi_import_0__ = await import("./proxy");
963963+ const __vi_import_1__ = await import("foo");
964964+965965+ console.log(__vi_import_1__.default);"
966966+ `)
933967 })
934968935969 // #10289
+5-3
packages/mocker/src/node/hoistMocks.ts
···107107 } = options
108108109109 // hoist at the start of the file, after the hashbang
110110- let hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0
110110+ const hashbangEnd = hashbangRE.exec(code)?.[0].length ?? 0
111111+ let hoistIndex = hashbangEnd
111112112113 let hoistedModuleImported = false
113114···535536 const utilityImports = [...usedUtilityExports]
536537 // "vi" or "vitest" is imported from a module other than "vitest"
537538 if (utilityImports.some(name => idToImportMap.has(name))) {
538538- s.prepend(API_NOT_FOUND_CHECK(utilityImports))
539539+ s.appendLeft(hashbangEnd, API_NOT_FOUND_CHECK(utilityImports))
539540 }
540541 // if "vi" or "vitest" are not imported at all, import them
541542 else if (utilityImports.length) {
542542- s.prepend(
543543+ s.appendLeft(
544544+ hashbangEnd,
543545 `import { ${[...usedUtilityExports].join(', ')} } from ${JSON.stringify(
544546 hoistedModule,
545547 )}\n`,