You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a Date or a Promise is created in the test environment context, those objects will fail an instanceof Date/instanceof Promise in the test runner context.
Context
I'm writing a library to wrap a pretty large SOAP API. When the library loads, a big chunk of XML must be parsed. This process takes around 4 seconds.
Instead of running this process for each test file, turning my test suite horribly slow, I opted to load this code once for the test suite, by placing the library initialization in a custom test environment setup and injecting the library through globals. This works fine and makes the suite much faster.
However, whenever a Date is returned from the lib, the instanceof operator has not the expected (in my opinion) behaviour.
As a consequence, the expect.any(Date) matcher will fail, leading to confusion as to why an object which looks like a Date, behaves like a Date, has a Date constructor is reported as not a Date :).
As demonstrated in my repro repo, this is not limited to Date but all other globals which are not injected by jest-test-environment-node (i.e Array, Promise; Buffer is fine though).
A possible fix would be to inject those globals in the runner context, but, as discussed in #5077, might not be desirable.
Anyway, feel free to mark this as wontfix. I just wanted to report it and possibly avoid headaches to other users who might encounter this issue and wonder why expect.any breaks with variables created during the test environment setup.
Environment
Linux x64
Node v10.9.0
Jest 23.5.0
The text was updated successfully, but these errors were encountered:
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
🐛 Bug Report
Summary
If a
Date
or aPromise
is created in the test environment context, those objects will fail aninstanceof Date
/instanceof Promise
in the test runner context.Context
I'm writing a library to wrap a pretty large SOAP API. When the library loads, a big chunk of XML must be parsed. This process takes around 4 seconds.
Instead of running this process for each test file, turning my test suite horribly slow, I opted to load this code once for the test suite, by placing the library initialization in a custom test environment setup and injecting the library through globals. This works fine and makes the suite much faster.
However, whenever a Date is returned from the lib, the
instanceof
operator has not the expected (in my opinion) behaviour.As a consequence, the
expect.any(Date)
matcher will fail, leading to confusion as to why an object which looks like aDate
, behaves like aDate
, has aDate
constructor is reported as not aDate
:).To reproduce
jest
oryarn test
Comments
As demonstrated in my repro repo, this is not limited to Date but all other globals which are not injected by jest-test-environment-node (i.e
Array
,Promise
;Buffer
is fine though).This leads to some hair pulling console output:
Workaround
A workaround is to inject those globals manually during test environment setup. See here : https://github.com/kouak/jest-custom-env-repro/blob/917a2d5bb04a625efc4b9a160efa94dac80bd1aa/testenv.js#L16-L19
A possible fix would be to inject those globals in the runner context, but, as discussed in #5077, might not be desirable.
Anyway, feel free to mark this as
wontfix
. I just wanted to report it and possibly avoid headaches to other users who might encounter this issue and wonder whyexpect.any
breaks with variables created during the test environment setup.Environment
The text was updated successfully, but these errors were encountered: