vitest: vi.mock not working in browser mode with test files outside of root

Describe the bug

I read it was implemented, but I can’t get it to work. It works as expected when run in node though.

Is there a special configuration required that I haven’t found in the docs?

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-pywprx8q?file=vite.config.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/browser: latest => 3.0.7 
    @vitest/ui: latest => 3.0.7 
    playwright: latest => 1.50.1 
    vite: latest => 6.2.0 
    vitest: latest => 3.0.7 
    webdriverio: latest => 9.10.1

Used Package Manager

npm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Great! In what version will this fix be released? to keep an eye out.

~Just checking the original repro again, it doesn’t look like root config actually matters. Mock is not working even after removing root from the config (both on Vitest 3.0.7 at the time of repro or the latest 3.1.1).~

Ah, I think I just hit a different bug which break mocking entirely. My directory had an empty space like vitest-dev-vitest-pywprx8q (2) and this made mocking to break. https://github.com/vitest-dev/vitest/issues/7788

@bteng22 Hmm, I just tested the latest Vitest 3.1.0 and it looks working now. Can you test it?

(Tested the OP’s repro with the latest and that one seems still broken).

I’m not sure if there’s a reason mock cannot work with this setup. I’ll treat it as a bug for now.