Skip to content

RollupError: Parse failure with unorthodox hoists #9424

@rChaoz

Description

@rChaoz

Describe the bug

Unorthodox uses of vi.hoisted() cause invalid code to be generated during the vitest:mocks transform, for example:

const mock = (await $.track_reactivity_loss(vi.hoisted(() => import("vitest-mock-extended").then((m) => m.mock))))();
const toaster = vi.hoisted(() => mock());

becomes

vi.hoisted(() => import("vitest-mock-extended").then((m) => m.mock))const toaster = vi.hoisted(() => mock());

const mock = (await __vi_import_0__.track_reactivity_loss())();

The unorthodox code is initially just a normal

const mock = await vi.hoisted(() => import("vitest-mock-extended").then((m) => m.mock))
const toaster = vi.hoisted(() => mock<Toaster>())

but top-level awaits are wrapped by Svelte in $.track_reactivity_loss in a previous transform.

Reproduction

Ensure Svelte is setup/configured for the project. Run a test file named whatever.svelte.test.ts containing:

import { vi } from "vitest";

const path = await vi.hoisted(() => import("node:path"));
const somePath = vi.hoisted(() => path.resolve("test"));

Leads to error:

RollupError: Parse failure: Expected ';', '}' or <eof>
At file: /src/lib/util/some.test.ts:1:66
  File: /src/lib/util/some.test.ts:1:66
  1  |  ...sted(() => import("/@id/__vite-browser-external:node:path"))const somePath = vi.hoisted(() => path.resolve("test")...
     |                                                                 ^                                                                                                                                                            
  2  |  /* clipboard-helper.svelte.test.ts generated by Svelte v5.46.1 */                                                                                                                                                           
  3  |

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700KF
    Memory: 8.53 GB / 31.86 GB
  Binaries:
    Node: 24.4.0 - C:\Users\Matei\AppData\Local\Temp\xfs-815d4b46\node.CMD
    Yarn: 4.6.0 - C:\Users\Matei\AppData\Local\Temp\xfs-815d4b46\yarn.CMD
    npm: 11.4.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 143.0.7499.170
    Edge: Chromium (140.0.3485.66)
    Firefox: 146.0.1 - C:\Program Files\Mozilla Firefox\firefox.exe
    Internet Explorer: 11.0.26100.7309
  npmPackages:
    @vitest/coverage-v8: 4.0.16 => 4.0.16 
    @vitest/ui: 4.0.16 => 4.0.16 
    vite: 7.3.1 => 7.3.1 
    vitest: 4.0.16 => 4.0.16 
    vitest-mock-extended: ^3.1.0 => 3.1.0

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions