vitest icon indicating copy to clipboard operation
vitest copied to clipboard

importActual is broken for Vitest Browser for WebKit via Playwright 1.55.1

Open aaronmaxlevy opened this issue 5 months ago • 2 comments

Describe the bug

importActual doesn't currently work in browser mode when using WebKit via Playwright 1.55.1. It worked fine with WebKit via Playwright 1.54.1. We are using Vitest 3.2.4. It works fine with Chromium and Firefox (also via Playwright).

The initial behavior I experienced was the following vague error message:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock
 ❯ ManualMockedModule.resolve ../../common/temp/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected][email protected][email protected]_bg6tt6rfcmq75nn6ebl6vtdzrq/node_modules/@vitest/mocker/dist/chunk-registry.js:160:24
 ❯ process.processTicksAndRejections node:internal/process/task_queues:105:5
 ❯ ../../common/temp/node_modules/.pnpm/@[email protected][email protected][email protected]_@[email protected][email protected]_te_fmp7q33g6k7xk2c3ndqr5ztwue/node_modules/@vitest/browser/dist/webdriver-KA1WiV0q.js:128:35
 ❯ RouteHandler._handleInternal ../../common/temp/node_modules/.pnpm/[email protected]/node_modules/playwright-core/lib/client/network.js:690:23
 ❯ RouteHandler._handleImpl ../../common/temp/node_modules/.pnpm/[email protected]/node_modules/playwright-core/lib/client/network.js:659:14
 ❯ RouteHandler.handle ../../common/temp/node_modules/.pnpm/[email protected]/node_modules/playwright-core/lib/client/network.js:653:12
 ❯ Page._onRoute ../../common/temp/node_modules/.pnpm/[email protected]/node_modules/playwright-core/lib/client/page.js:145:23

Caused by: Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock
 ❯ resolve ../../../../../../__vitest_browser__/tester-BYDMHqQ9.js:2011:36

I ended up modifying tester-BYDMHqQ9.js (in absence of a better way to debug this AFAIK) to throw the actual error, which turned out to be:

Caused by: Error: [vitest] Cannot resolve "./api.js" imported from "/Users/REDACTED/workspace/REDACTED/common/temp/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_@[email protected]_h_xuzwyto4rlaufebhwsxt6lodaa/node_modules/vitest/dist/chunks/vi.bdSIJ99Y.js"
 ❯ importActual ../../../../../../__vitest_browser__/tester-BYDMHqQ9.js:3195:22

I then modified importActual in tester-BYDMHqQ9.js to always throw an error, even if resolved is non-null (in order to see what importer value is used in those browsers), and tested with both Chromium and Firefox (via Playwright), which both correctly provided /Users/REDACTED/workspace/REDACTED/REDACTED/REDACTED/src/REDACTED/REDACTED.test.ts as the importer path.

This is as far as I got in debugging this, but from what I can tell, the importer path is determined based on parsing a stack trace, and I believe that the WebKit Browser that ships with Playwright 1.55.1 isn't providing the same stack trace data as e.g. Playwright 1.54.1, which is causing this issue (incorrect importer path).

I am a bit beyond my depth on debugging this further though.

Reproduction

See https://github.com/aaronmaxlevy/vitest-bug-example-1

System Info

System:
    OS: macOS 15.7.1
    CPU: (16) arm64 Apple M4 Max
    Memory: 72.22 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.20.0 - /Users/REDACTED/.nvm/versions/node/v22.20.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.9.3 - /Users/REDACTED/.nvm/versions/node/v22.20.0/bin/npm
    pnpm: 9.15.9 - /Users/REDACTED/.nvm/versions/node/v22.20.0/bin/pnpm
  Browsers:
    Chrome: 142.0.7444.60
    Safari: 26.0.1
  npmPackages:
    @vitest/browser: ^3.2.4 => 3.2.4 
    @vitest/coverage-istanbul: ^3.2.4 => 3.2.4 
    @vitest/coverage-v8: ^3.2.4 => 3.2.4 
    @vitest/ui: ^3.2.4 => 3.2.4 
    playwright: 1.55.1 => 1.55.1 
    vite: ^7.0.4 => 7.0.4 
    vitest: ^3.2.4 => 3.2.4

Used Package Manager

pnpm

Validations

aaronmaxlevy avatar Oct 30 '25 19:10 aaronmaxlevy

Hello @aaronmaxlevy. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

github-actions[bot] avatar Oct 30 '25 21:10 github-actions[bot]

I have added a reproduction in GitHub repo https://github.com/aaronmaxlevy/vitest-bug-example-1

aaronmaxlevy avatar Oct 31 '25 22:10 aaronmaxlevy