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

A date created in the test environment and then attached to the global object is not an instance of Date #7246

Closed
nikgraf opened this issue Oct 23, 2018 · 3 comments

Comments

@nikgraf
Copy link
Contributor

nikgraf commented Oct 23, 2018

🐛 Bug Report

A date created in the test environment and then attached to the global object is not an instance of Date.

test environment

const NodeEnvironment = require('jest-environment-node')

class MyEnvironment extends NodeEnvironment {
  async setup() {
    await super.setup()
    this.global.someDate = new Date()
  }

test

test("some date to be instance of Date", () => {
  expect(global.someDate instanceof Date).toBe(true); // the result is false
});

To Reproduce

I made an example repository.

Steps to reproduce the behavior:

  • attach a Date to global inside a custom test environment
  • use instanceof on that variable in the the test

Note: In our case we instantiated a Knex client which we attached to global to instantiate it only once and destroy it only once. Once we ran a query where one entry was a date, but instanceof didn't work.

Expected behavior

global.someDate instanceof Date should be true

Link to repl or repo (highly encouraged)

git clone git@github.com:nikgraf/jest-bug-example.git
yarn
yarn test

Result:

➜  jest-example git:(master) yarn test
yarn run v1.10.1
$ jest
 FAIL  ./index.test.js
  ✓ adding two numbers (2ms)
  ✕ some date to be instance of Date (6ms)

  ● some date to be instance of Date

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

       8 |   expect(new Date() instanceof Date).toBe(true);
       9 |   // NOTE this is an object containing methods like getMonth, but isn't a Date instance anymore
    > 10 |   expect(global.someDate instanceof Date).toBe(true);
         |                                           ^
      11 | });

      at Object.toBe (index.test.js:10:43)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        0.328s, estimated 1s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Run npx envinfo --preset jest

Paste the results here:

npx envinfo --preset jest
npx: installed 1 in 2.133s

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 
@SimenB
Copy link
Member

SimenB commented Oct 23, 2018

This is expected - the global Date inside and outside of the sandbox is not the same.

You can work around it by doing this.global.someDate = new this.global.Date().

If you want to create something in the custom environment using the same globals, you need to execute it inside of the vm this.executeScript.

Also sorta dupe of #2549, although your use case (creating something outside of the sandbox and using it inside of it) is not really supported - it breaks the abstraction.

You might consider using setupFiles instead

@SimenB SimenB closed this as completed Oct 23, 2018
@nikgraf
Copy link
Contributor Author

nikgraf commented Oct 25, 2018

@SimenB thanks for the clarification

From what I understood setupFiles won't work, because knex keeps a DB connection open and then the test execution never finishes.

Regarding your workaround. In which file would I place this.global.someDate = new this.global.Date()?

Also I'm not fully understanding this.executeScript. I was looking up the Node docs for vm, but there is no executeScript afaik.

@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