Skip to content

fix: stale mock metadata breaks automocking with isolate:false (fix #10145)#10541

Open
kade-robertson wants to merge 3 commits into
vitest-dev:mainfrom
kade-robertson:fix-10145
Open

fix: stale mock metadata breaks automocking with isolate:false (fix #10145)#10541
kade-robertson wants to merge 3 commits into
vitest-dev:mainfrom
kade-robertson:fix-10145

Conversation

@kade-robertson

Copy link
Copy Markdown

Description

When isolate: false is set and two test files mock the same module differently (one with a factory, one with automocking), the automock is silently skipped if the factory-mock file runs first. This happens because fetchModule bakes mockedModule (with code: '') into the module node's meta, and with isolate: false this metadata persists across test files. When a later file registers an automock for the same module, cachedRequest was using the stale manual mock from meta instead of the current automock from the registry.

The fix adds an additional check after the early stale / self-import returns to see if currentMock and mockedModule differ, we re-fetch.

A resolve.alias is required to trigger the bug.

Resolves #10145

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

mockedModule,
)
if (currentMock !== mockedModule) {
const freshNode = await this.fetchModule(injectQuery(url, '_vitest_original'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a manual or redirect mock, then this is not needed because it won't be used to autospy/automock

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly I believe f394cb4 addresses this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automocking silently breaks with isolate: false + mixed mock types and resolve aliases

2 participants