Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Automock breaking instanceof comparisons in transitive dependencies of mocked module #15401

Open
liamjones opened this issue Dec 5, 2024 · 0 comments

Comments

@liamjones
Copy link

Version

29.7.0

Steps to reproduce

  1. Clone https://github.com/liamjones/RealmMockIssue
  2. npm install
  3. npm run test

Expected behavior

Test passes

Actual behavior

Test will fail with Illegal constructor: Results objects are read from managed objects only. (this comes from an instanceof check failing inside the realm module)

Additional context

Commenting out the auto-mocking of the hasRatedThisVersion file that has an import Realm from 'realm' inside it: https://github.com/liamjones/RealmMockIssue/blob/main/__tests__/realm.test.ts#L6 results in the instanceof check passing.

Providing a factory function for the mock (so auto-mocking doesn't kick in) also fixes the instanceof check.

I originally reported this to the Realm project (realm/realm-js#6122 (comment)) and one of the realm module devs has provided a more detailed explanation of how/where the instanceof check is failing in their code: realm/realm-js#6122 (comment)

After digging into the auto-mock code & searching for existing Jest issues I found the bug re differing globals across VM contexts: #2549 (comment). It seems similar, but I'm not sure it's the same issue. Realm itself isn't being mocked, just a module that imports it.

I do understand that if a Realm.Results in one VM context was compared to another via instanceof then the check would fail, and this is intended behaviour (nodejs/node-v0.x-archive#1277). However, I don't get how, if Realm is being initialised in a new VM context under the auto-mocked module, it'd be 'leaking' out to the version of the realm module in the test context.

Environment

System:
    OS: macOS 15.1.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 20.11.1 - ~/.volta/tools/image/node/20.11.1/bin/node
    Yarn: 4.0.2 - ~/.volta/tools/image/yarn/4.0.2/bin/yarn
    npm: 10.2.4 - ~/.volta/tools/image/node/20.11.1/bin/npm
    pnpm: 9.1.1 - ~/.volta/bin/pnpm
    bun: 1.1.38 - /usr/local/bin/bun
  npmPackages:
    jest: ^29.6.3 => 29.7.0
@liamjones liamjones changed the title [Bug]: Automock breaking instanceof in mocked module's transitive dependencies [Bug]: Automock breaking instanceof comparisons ins transitive dependencies of mocked module Dec 5, 2024
@liamjones liamjones changed the title [Bug]: Automock breaking instanceof comparisons ins transitive dependencies of mocked module [Bug]: Automock breaking instanceof comparisons in transitive dependencies of mocked module Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant