Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/mocker/src/node/hoistMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export function hoistMocks(
} = options

// hoist at the start of the file, after the hashbang
let hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0
const hashbangEnd = hashbangRE.exec(code)?.[0].length ?? 0
let hoistIndex = hashbangEnd

let hoistedModuleImported = false

Expand Down Expand Up @@ -535,11 +536,12 @@ export function hoistMocks(
const utilityImports = [...usedUtilityExports]
// "vi" or "vitest" is imported from a module other than "vitest"
if (utilityImports.some(name => idToImportMap.has(name))) {
s.prepend(API_NOT_FOUND_CHECK(utilityImports))
s.appendLeft(hashbangEnd, API_NOT_FOUND_CHECK(utilityImports))
}
// if "vi" or "vitest" are not imported at all, import them
else if (utilityImports.length) {
s.prepend(
s.appendLeft(
hashbangEnd,
`import { ${[...usedUtilityExports].join(', ')} } from ${JSON.stringify(
hoistedModule,
)}\n`,
Expand Down
42 changes: 38 additions & 4 deletions test/core/test/injector-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,21 +915,55 @@
// #8002
test('with hashbang', () => {
expect(
hoistSimpleCodeWithoutMocks(
hoistSimpleCode(
`#!/usr/bin/env node
vi.mock('foo');
console.log("it can parse the hashbang")`,
),
).toMatchInlineSnapshot(`undefined`)
).toMatchInlineSnapshot(`
"#!/usr/bin/env node
import { vi } from "vitest"
vi.mock('foo');
console.log("it can parse the hashbang")"
`)
})

test('import hoisted after hashbang', () => {
expect(
hoistSimpleCodeWithoutMocks(
hoistSimpleCode(
`#!/usr/bin/env node
vi.mock('foo');
console.log(foo);
import foo from "foo"`,
),
).toMatchInlineSnapshot(`undefined`)
).toMatchInlineSnapshot(`
"#!/usr/bin/env node
import { vi } from "vitest"
vi.mock('foo');
const __vi_import_0__ = await import("foo");
console.log(__vi_import_0__.default);"
`)
})

test('import hoisted after hashbang', () => {
expect(
hoistSimpleCode(
`#!/usr/bin/env node
import { vi } from './proxy'
vi.mock('foo');
console.log(foo);
import foo from "foo"`,
),
).toMatchInlineSnapshot(`
"#!/usr/bin/env node

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") }
__vi_import_0__.vi.mock('foo');
const __vi_import_0__ = await import("./proxy");
const __vi_import_1__ = await import("foo");

console.log(__vi_import_1__.default);"
`)
})

// #10289
Expand Down Expand Up @@ -1256,7 +1290,7 @@
vi.mock(await import(somePath), () => {});
vi.mock(await import(\`./path\`), () => {});
`),
).toMatchInlineSnapshot(`

Check failure on line 1293 in test/core/test/injector-mock.test.ts

View workflow job for this annotation

GitHub Actions / Rolldown&Test: node-22, macos-latest

[threads] test/injector-mock.test.ts > transform > handles dynamic import as the first argument

Error: Snapshot `transform > handles dynamic import as the first argument 1` mismatched - Expected + Received - "import { vi } from "vitest" - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {}); - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {});" + undefined ❯ test/injector-mock.test.ts:1293:7

Check failure on line 1293 in test/core/test/injector-mock.test.ts

View workflow job for this annotation

GitHub Actions / Rolldown&Test: node-22, macos-latest

[vmThreads] test/injector-mock.test.ts > transform > handles dynamic import as the first argument

Error: Snapshot `transform > handles dynamic import as the first argument 1` mismatched - Expected + Received - "import { vi } from "vitest" - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {}); - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {});" + undefined ❯ test/injector-mock.test.ts:1293:7

Check failure on line 1293 in test/core/test/injector-mock.test.ts

View workflow job for this annotation

GitHub Actions / Rolldown&Test: node-22, macos-latest

[forks] test/injector-mock.test.ts > transform > handles dynamic import as the first argument

Error: Snapshot `transform > handles dynamic import as the first argument 1` mismatched - Expected + Received - "import { vi } from "vitest" - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path') - vi.mock(somePath) - vi.mock(`./path`) - vi.mock('./path'); - vi.mock(somePath); - vi.mock(`./path`); - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}) - vi.mock(somePath, () => {}) - vi.mock(`./path`, () => {}) - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {}); - vi.mock('./path', () => {}); - vi.mock(somePath, () => {}); - vi.mock(`./path`, () => {});" + undefined ❯ test/injector-mock.test.ts:1293:7
"import { vi } from "vitest"
vi.mock('./path')
vi.mock(somePath)
Expand Down
Loading