Skip to content

fix(spy): point ESM namespace spy error to module mocking docs - #10707

Merged
sheremet-va merged 1 commit into
vitest-dev:mainfrom
ryandiginomad:fix/9467-spyon-esm-error-docs-link
Jul 29, 2026
Merged

fix(spy): point ESM namespace spy error to module mocking docs#10707
sheremet-va merged 1 commit into
vitest-dev:mainfrom
ryandiginomad:fix/9467-spyon-esm-error-docs-link

Conversation

@ryandiginomad

Copy link
Copy Markdown
Contributor

Description

The Cannot spy on export "…". Module namespace is not configurable in ESM error links users to a browser-mode-only docs anchor (/guide/browser/#limitations). But this error fires in Node too — the reproduction in the issue (vi.spyOn(lodash, 'after')) has nothing to do with browser mode, so the link sends Node users somewhere irrelevant.

This points the message at the environment-agnostic module mocking guide (/guide/mocking/modules#mocking-a-module) instead — the standard vi.mock(path, { spy: true }) solution — following @sheremet-va's steer in the issue to "lean into the standard" rather than suggest a mode-specific config. The message body and the guard are unchanged; only the link moves. No environment branching is added.

I used the #mocking-a-module section anchor (not the bare page) to match the deep-linking convention already used for the sibling module-mocking error in packages/mocker/src/node/hoistMocks.ts.

Resolves #9467

Tests

Verified locally (fix reverted → test fails, fix applied → passes):

  • Node (test/unit): added a regression test in vi.spyOn() edge cases that fakes a non-configurable ESM namespace and asserts the message points at the mocking guide and not at /guide/browser. This is the path the browser snapshot test didn't cover.
  • Browser (test/browser): updated the existing inline snapshot to the new link.

Open questions for maintainers

  • The { spy: true } remedy on the target page currently sits inside a "Browser Mode Support" danger callout — a Node reader landing there might still hesitate. Happy to open a small follow-up to make that section's framing environment-agnostic if you'd like, but kept it out of this PR to keep the change minimal.
  • I left the error message body as-is (link only). If you'd prefer it to inline the vi.mock(path, { spy: true }) hint directly, I can do that too — just wanted to avoid over-reaching (the earlier chore: improve spyOn ESM namespace error guidance #9505 went that route).
  • The issue also notes the v3→v4 behavior change isn't in the migration guide. That felt separable from this message fix, so I left it out — happy to follow up if useful.

🤖 AI assistance

Developed with Claude Code. I diagnosed the throw site, verified the docs anchor lands on the right content, and confirmed the fix with a local red→green test run. I've reviewed every line of this diff.

@netlify

netlify Bot commented Jul 3, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 231ca0b
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a5a49a2e49c7a0008492030
😎 Deploy Preview https://deploy-preview-10707--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

The 'Cannot spy on export' error fires in Node too, but its message
linked users to the browser-mode-only docs anchor
(/guide/browser/#limitations). Point it at the environment-agnostic
module mocking guide (/guide/mocking/modules#mocking-a-module) instead,
matching the standard solution the maintainers recommend.

Fixes vitest-dev#9467
@ryandiginomad
ryandiginomad force-pushed the fix/9467-spyon-esm-error-docs-link branch from ccef521 to 231ca0b Compare July 17, 2026 15:26
@ryandiginomad

Copy link
Copy Markdown
Contributor Author

Quick check-in on this one — it's been in the queue a couple of weeks. I've rebased it on main and CI is green.

It's a small fix following the direction @sheremet-va outlined in #9467: the vi.spyOn ESM namespace error currently points Node users at browser-mode docs, so this points it at the environment-agnostic mocking guide instead. Happy to adjust the wording or the target anchor if you'd prefer something different.

@sheremet-va
sheremet-va merged commit d6ceca9 into vitest-dev:main Jul 29, 2026
27 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading error messages relating to spyOn modules. - "Cannot spy on export "[X]". Module namespace is not configurable in ESM"

2 participants