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

Date or Promise instantiated in test environment will not pass expect.any(Date|Promise) #6877

Closed
kouak opened this issue Aug 21, 2018 · 2 comments

Comments

@kouak
Copy link

kouak commented Aug 21, 2018

🐛 Bug Report

Summary

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 :).

To reproduce

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:
capture d ecran de 2018-08-22 01-14-29

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 why expect.any breaks with variables created during the test environment setup.

Environment

  • Linux x64
  • Node v10.9.0
  • Jest 23.5.0
@SimenB
Copy link
Member

SimenB commented Aug 23, 2018

This is a duplicate of #2549.

Help on #5995 would be greatly appreciated!

@SimenB SimenB closed this as completed Aug 23, 2018
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants