Skip to content

jest-compat: jest.mock through a helper's parameter is handled differently for jest and a renamed jest #844

Open

Description

@danfry1

jestMockTransform() treats a helper's parameter differently depending on its name. Once #843 lands:

function setup(jest) { jest.mock('./a'); }          // rewritten to vi.doMock('./a'), registered
function setup(mockJest) { mockJest.mock('./a'); }  // left as is (the parameter shadows the import)

Without the rewrite, the call runs at run time on the jest object, which is Vitest's vi.mock. That call does not reach the Node-side registry (__vnJestMock) and does not apply Jest's factory interop. A later require('./a'), or a Node-owned package, can then load the real module, where Jest would return the mock. The jest case has always been rewritten, and #843 keeps it that way to avoid a behaviour change in a fix release.

Possible direction: treat a call through any parameter or local that is called with the jest object as non-hoisted (doMock/doUnmock semantics, registered), or fail with JEST_BINDING_NOT_FOLLOWED. Either way, a parameter named jest and one named mockJest should give the same result. The runtime consequence should be confirmed with a native test before choosing.

Activity

  1. added this to the 0.16.0 milestone on Nov 14, 2026
  2. added
    bug
    area:jest-compat
    priority:low
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:jest-compatbugpriority:low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions