Skip to content

Jest seems to share JSdom environments accross tests #7654

@julienw

Description

@julienw

🐛 Bug Report

From the documentation in https://jestjs.io/docs/en/configuration#testenvironment-string, the test environment should be sandboxed:

Note: TestEnvironment is sandboxed. Each test suite will trigger setup/teardown in their own TestEnvironment.

But in our tests we're mocking some prototype methods and it looks like that they're shared, and this produces intermittents.

To Reproduce

Steps to reproduce the behavior:

  1. Run these commands:
git clone https://github.com/julienw/testcase-jest1
cd testcase-jest1
yarn
yarn test

This repository contains several times the same tests:

  • testcaseX.test.js mocks HTMLElement.prototype.getBoundingClientRect
  • defaultX.test.js doesn't mock this method
  • all files testcaseX.test.js have the same content, so they should have the same result
  • all files defaultX.test.js have the same content, so they should have the same result

Also, the configuration contains:

    "restoreMocks": true,
    "resetMocks": true,

But this doesn't seem to make a difference in the result. What does make a difference is these changes: https://github.com/julienw/testcase-jest1/compare/reset-mocks. So maybe the issue is the implementation for resetMocks and not the test environment?

For me this fails nearly 100% of the time, but because this is likely a frequent intermittent you might need to repeat the test several times.

Here is the error I get:

 FAIL  ./default1.test.js
  ✕ returns the default bounding box (6ms)

  ● returns the default bounding box

    expect(received).toEqual(expected)

    Expected value to equal:
      {"bottom": 0, "height": 0, "left": 0, "right": 0, "top": 0, "width": 0}
    Received:
      {"bottom": 300, "height": 300, "left": 0, "right": 300, "top": 0, "width": 300, "x": 0, "y": 0}

    Difference:

    - Expected
    + Received

      Object {
    -   "bottom": 0,
    -   "height": 0,
    +   "bottom": 300,
    +   "height": 300,
        "left": 0,
    -   "right": 0,
    +   "right": 300,
        "top": 0,
    -   "width": 0,
    +   "width": 300,
    +   "x": 0,
    +   "y": 0,
      }

      3 |   document.body.appendChild(element);
      4 | 
    > 5 |   expect(element.getBoundingClientRect()).toEqual({
        |                                           ^
      6 |     bottom: 0,
      7 |     height: 0,
      8 |     left: 0,

      at Object.toEqual (default1.test.js:5:43)

Link to repl or repo (highly encouraged)

https://github.com/julienw/testcase-jest1

Run npx envinfo --preset jest

Paste the results here:

npx: 1 installé(s) en 1.166s

  System:
    OS: Linux 4.18 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
  Binaries:
    Node: 9.11.1 - ~/.nvm/versions/node/v9.11.1/bin/node
    Yarn: 1.12.3 - ~/.yarn/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v9.11.1/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions