Skip to content

Commit 65432a7

Browse files
authored
fix(mock): inject helpers after hashbang if present (#9545)
1 parent 5b16483 commit 65432a7

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

packages/mocker/src/node/hoistMocks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ export function hoistMocks(
107107
} = options
108108

109109
// 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
111112

112113
let hoistedModuleImported = false
113114

@@ -535,11 +536,12 @@ export function hoistMocks(
535536
const utilityImports = [...usedUtilityExports]
536537
// "vi" or "vitest" is imported from a module other than "vitest"
537538
if (utilityImports.some(name => idToImportMap.has(name))) {
538-
s.prepend(API_NOT_FOUND_CHECK(utilityImports))
539+
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) {
542-
s.prepend(
543+
s.appendLeft(
544+
hashbangEnd,
543545
`import { ${[...usedUtilityExports].join(', ')} } from ${JSON.stringify(
544546
hoistedModule,
545547
)}\n`,

test/core/test/injector-mock.test.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,21 +915,55 @@ export default (function getRandom() {
915915
// #8002
916916
test('with hashbang', () => {
917917
expect(
918-
hoistSimpleCodeWithoutMocks(
918+
hoistSimpleCode(
919919
`#!/usr/bin/env node
920+
vi.mock('foo');
920921
console.log("it can parse the hashbang")`,
921922
),
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+
`)
923929
})
924930

925931
test('import hoisted after hashbang', () => {
926932
expect(
927-
hoistSimpleCodeWithoutMocks(
933+
hoistSimpleCode(
928934
`#!/usr/bin/env node
935+
vi.mock('foo');
929936
console.log(foo);
930937
import foo from "foo"`,
931938
),
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+
`)
933967
})
934968

935969
// #10289

0 commit comments

Comments
 (0)