[Bug]: Automock breaking instanceof
comparisons in transitive dependencies of mocked module
#15401
Labels
instanceof
comparisons in transitive dependencies of mocked module
#15401
Version
29.7.0
Steps to reproduce
npm install
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 therealm
module)Additional context
Commenting out the auto-mocking of the
hasRatedThisVersion
file that has animport Realm from 'realm'
inside it: https://github.com/liamjones/RealmMockIssue/blob/main/__tests__/realm.test.ts#L6 results in theinstanceof
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 viainstanceof
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
The text was updated successfully, but these errors were encountered: