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

instanceof doesn't work with Jest and rewire #164

Open
georgezlei opened this issue May 12, 2019 · 2 comments
Open

instanceof doesn't work with Jest and rewire #164

georgezlei opened this issue May 12, 2019 · 2 comments

Comments

@georgezlei
Copy link

instanceof doesn't work in Jest when being used on an object returned from Rewire. Please check out codes below.

file rewired.test.js

const rewire = require('rewire')
const code = rewire('./rewired')

const here = {}
const there = code.__get__('another')

test('test', () => {
  expect(here instanceof Object).toBeTruthy()
  expect(there instanceof Object).toBeTruthy()
})

file rewired.js

const another = {}

It turns out that Jest uses new vm context for each test to simulate a new browser window. That caused all builtins such as Object and Array are created again. And for some reason, the objects created by rewire are cross-context and based on another Object. So the instanceof test can never have correct result.

Here is an example.

const vm = require('vm')
const code = `
const rewire = require("rewire");
const code = rewire("./rewired")

const obj = code.__get__("Object");

console.log(obj === Object);
`;

eval(code)
vm.runInNewContext(code, {require, console})

The result is

true
false
@robross0606
Copy link

I hit this same issue

@nktnet1
Copy link

nktnet1 commented Oct 12, 2023

I had the same issue, so I wrote jewire.

1

It's a niche library that clones objects and arrays at runtime solely for Jest purposes :D. Hope this helps someone out there.

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

No branches or pull requests

3 participants