Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upJest globals differ from Node globals #2549
Comments
thomashuston
referenced this issue
Jan 10, 2017
Closed
Jest globals different from source code globals? #2048
This comment has been minimized.
This comment has been minimized.
This is likely due to the behavior of Does Jest do anything to try to avoid this right now? |
This comment has been minimized.
This comment has been minimized.
PlasmaPower
commented
Mar 1, 2017
I came across the exact scenario. It's very hard to diagnose. I also came across it with Error objects. Writing wrapper workarounds for this is getting annoying. From the linked nodejs issue:
However, |
This comment has been minimized.
This comment has been minimized.
Jest team- should the node (and maybe jsdom) environment(s) be changed to put things like Alternatively, maybe babel-jest could transform |
This comment has been minimized.
This comment has been minimized.
PlasmaPower
commented
Mar 1, 2017
I don't like the babel-jest idea, if something like that is implemented it should be its own plugin. Other than that, I agree. |
This comment has been minimized.
This comment has been minimized.
We can't pull in the data structures from the parent context because we want to sandbox every test. If you guys could enumerate the places where these foreign objects are coming from, we can wrap those places and emit the correct instances. For example, if |
This comment has been minimized.
This comment has been minimized.
Is there any risk to the sandboxing added other than "if someone messes with these objects directly, it will affect other tests"? Or is there something inherent in the way the contexts are set up that would make this dangerous passively? Just trying to understand. I'd guess that |
This comment has been minimized.
This comment has been minimized.
It's one of the guarantees of Jest that two tests cannot conflict with each other, so we cannot change it. The question is where you are getting your Error and Arrays from that are causing trouble. |
This comment has been minimized.
This comment has been minimized.
PlasmaPower
commented
Mar 2, 2017
They come from node native libraries like fs or http. |
This comment has been minimized.
This comment has been minimized.
Ah, hmm, that's a good point. It works for primitives but not that well for errors or arrays :( |
This comment has been minimized.
This comment has been minimized.
What if jest transformed |
This comment has been minimized.
This comment has been minimized.
meh, I'm not sure I love that :( |
This comment has been minimized.
This comment has been minimized.
We could override |
This comment has been minimized.
This comment has been minimized.
I'm ok if this feature only works in newer versions of node. It seems much cleaner to me; assuming it doesn't have negative performance implications. |
This comment has been minimized.
This comment has been minimized.
Assuming performance seems fine, which globals should it be applied to? |
This comment has been minimized.
This comment has been minimized.
Yeah, that sounds like a good start. |
This comment has been minimized.
This comment has been minimized.
I may be able to tackle a PR for this this weekend. I'm assuming we want it in both the node and jsdom environments? |
This comment has been minimized.
This comment has been minimized.
I've started work on this in https://github.com/suchipi/jest/tree/instanceof_overrides, but am having difficulty reproducing the original issue. @PlasmaPower or @thomashuston do you have a minimal repro I could test against? |
This comment has been minimized.
This comment has been minimized.
thymikee
referenced this issue
Mar 6, 2017
Open
Jest mock module failed when mocking mongoose model object #3073
thymikee
added
Discussion
🚀 Enhancement
labels
Mar 6, 2017
This comment has been minimized.
This comment has been minimized.
joedynamite
commented
Mar 7, 2017
Not sure if it is 100% related or not but I have issues with exports not being considered |
This comment has been minimized.
This comment has been minimized.
PlasmaPower
commented
Mar 7, 2017
@joedynamite sounds like the same issue |
This comment has been minimized.
This comment has been minimized.
PlasmaPower
commented
Mar 7, 2017
Why not everything? I'm assuming performance won't be an issue as instanceof shouldn't be called often. |
This comment has been minimized.
This comment has been minimized.
jakeorr
commented
May 18, 2017
I ran into a related issue with Express+Supertest+Jest. The 'set-cookie' header comes in with all cookies in a single string rather than a string for each cookie. Here is a reproduction case with the output I'm seeing with Jest and with Mocha (it works with mocha): #3547 (comment) |
This comment has been minimized.
This comment has been minimized.
rexxars
commented
Jul 25, 2017
•
Just spent a couple of hours trying to figure out what happened when an app failed in weird ways because of an Basically, http errors seem to not be instances of Error, which is very frustrating. Very simple, reproducible test case here. |
This comment has been minimized.
This comment has been minimized.
L0stSoul
commented
May 21, 2018
•
@SimenB small example with Array(simplified one): it.only('multiple parameters', function () {
let url = require('url');
let query = url.parse('https://www.rakuten.co.jp/?f=1&f=2&f=3', true).query;
console.log(query.f); // [ '1', '2', '3' ]
console.log(query.f instanceof Array); //false
}); |
This comment has been minimized.
This comment has been minimized.
L0stSoul
commented
May 21, 2018
•
btw i'm not fully understand why this labeled as "enchantment" instead of "bug" - Changing behaviour of |
SimenB
referenced this issue
Jul 6, 2018
Open
mockResolvedValue / mockRejectedValue should returns a real Promise #6645
This comment has been minimized.
This comment has been minimized.
Bradcomp
commented
Jul 18, 2018
Is there any progress on this? This is leading to a very frustrating situation in one of my projects right now where a third party library is doing an |
This comment has been minimized.
This comment has been minimized.
Morikko
commented
Aug 17, 2018
For the one willing to parse cookies with the extension |
SimenB
referenced this issue
Aug 23, 2018
Closed
Date or Promise instantiated in test environment will not pass `expect.any(Date|Promise)` #6877
thymikee
referenced this issue
Aug 26, 2018
Closed
"Bound" function does not pass isInstanceOf(Function) #6893
This comment has been minimized.
This comment has been minimized.
ssetem
commented
Sep 20, 2018
•
Found a solution, we were having issues testing multiple Step 1 Object.defineProperty(Array, Symbol.hasInstance, {
value(target) {
return Array.isArray(target)
}
});
module.exports = require('jest-environment-node') Step 2 jest --testEnvironment ./utils/ArrayFixNodeEnvironment.js |
SimenB
referenced this issue
Sep 30, 2018
Open
Encoded text is not recognized as Unit8Array in testEnvironment "node" #7077
kurtzur
referenced this issue
Oct 5, 2018
Closed
Invalid prop of type `Map` supplied to Page, expected instance of `Map`. #221
Oct 16, 2018
This was referenced
This comment has been minimized.
This comment has been minimized.
meltedspark
commented
Oct 27, 2018
@ssetem You're a lifesaver, thank you! |
orta
added
the
Has Bounty
label
Dec 29, 2018
This comment has been minimized.
This comment has been minimized.
Hey folks, to get this moving forwards - we've decided to put a $599 bounty on this issue from our OpenCollective account. If you're interested in doing some Open Source good-will and get paid for it, we're looking to have this shipped to production. There are a few ideas already e.g. #5995 which you could take over, or you could start from fresh too To get the bounty you would need to submit an expense via OpenCollective, here's their FAQ and I'll help that part of the process |
SimenB
referenced this issue
Jan 11, 2019
Closed
[bug] jest.fn() instanceof Function === false #6329
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@jamesgeorge007 that's awesome! Not sure what information you're after - there's a bunch of reproducing examples in this issue (and linked) ones. You can also see my PR #5995 and take a look at fixing the errors from its CI run. I can rebase that PR now so it's fresh, though Any questions in particular? |
Jan 27, 2019
This was referenced
This comment has been minimized.
This comment has been minimized.
dhans
commented
Feb 6, 2019
Unclear if we were hitting exactly the same bug - but upgrading the test environment to Node.js 10 resolved an issue for us where tests running under jest using supertest were failing to find the existing session, using multiple cookies. |
This comment has been minimized.
This comment has been minimized.
That's expected, Node fixed their code to use That's just a symptom though, the underlying issue is not solved |
This comment has been minimized.
This comment has been minimized.
RLovelett
commented
Feb 6, 2019
Unfortunately @ssetem's work-around does not seem to be working for console.log('ArrayBufferFixNodeEnvironment');
// Fix for Jest in node v8.x and v10.x
Object.defineProperty(ArrayBuffer, Symbol.hasInstance, {
value(inst) {
return inst && inst.constructor && inst.constructor.name === 'ArrayBuffer';
}
});
module.exports = require('jest-environment-node') AFAICT it never calls the
|
thomashuston commentedJan 10, 2017
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
After making a request with Node's
http
package, checking if one of the response headers is aninstanceof Array
fails because theArray
class used insidehttp
seems to differ from the one available in Jest's VM.I specifically came across this when trying to use
node-fetch
in Jest to verify that cookies are set on particular HTTP responses. Theset-cookie
header hits this condition and fails to pass in Jest https://github.com/bitinn/node-fetch/blob/master/lib/headers.js#L38This sounds like the same behavior reported in #2048; re-opening per our discussion there.
If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can
yarn install
andyarn test
.https://github.com/thomas-huston-zocdoc/jest-fetch-array-bug
What is the expected behavior?
The global
Array
class instance in Jest should match that of Node's packages so type checks behave as expected.I've submitted a PR to
node-fetch
switching frominstanceof Array
toArray.isArray
to address the immediate issue, but the Jest behavior still seems unexpected and it took quite a while to track down.Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
I am using the default Jest configuration (I have not changed any settings in my
package.json
).Jest - 18.1.0
Node - 6.9.1 (also tested in 4.7.0 and saw the same error)
npm - 3.10.8
OS - Mac OS X 10.11.6