Skip to content

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

@kouak

Description

@kouak

🐛 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions