The issue involves complex interaction between Svelte's compilation and Vitest's mocking system.
The problem involves Svelte's compilation output breaking Vitest's hoisting functionality when using top-level await. The fix would need to modify Svelte's compilation output to preserve Vitest's expected syntax while maintaining reactivity tracking. The main challenge is understanding both systems' transformation pipelines.
See https://github.com/vitest-dev/vitest/issues/9424
In short:
When using Vitest, one can use vi.hoisted() to execute code before top-level imports (which Vitest transforms into await import()s placed after the hoisted declaration). However, for this to work, vi.hoisted needs to be used as part of a top-level variable declaration, like
const num = vi.hoisted(() => 123)
const num2 = await vi.hoisted(async () => 456)
However, this code is first transformed by Svelte into
const num = vi.hoisted(() => 123)
const num2 = (await $.track_reactivity_loss(vi.hoisted(async () => 456)))()
which Vitest cannot process.
In the end, it is impossible to use await vi.hoisted() together with Svelte tests.
https://github.com/rChaoz/svelte-vitest-hoisted-bug-repro
blocking an upgrade
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!