question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document is defined in `@jest-environment node`

See original GitHub issue

🐛 Bug Report

@jest-environment node doesn’t correctly set document, which we are using to determine the environment:

// env.js
export const isBrowser = (typeof window !== 'undefined'
                       && typeof document !== 'undefined'
                       && typeof document.createElement === 'function')

To Reproduce

Run jest on a single using the default "testEnvironment": "jsdom" then specify the node environment for a test.

// passes
test('detects browser-like environment', () => {
  expect(typeof document).not.toBe('undefined');
  expect(typeof window).not.toBe('undefined');
});


/**
 * @jest-environment node
 */
test('detects node environment', () => {
  expect(typeof document).toBe('undefined'); // does not pass
  expect(typeof window).toBe('undefined');
});

The second test will fail:

FAIL  ./env-test.js
  ✓ detects browser-like environment (5ms)
  ✕ detects node environment (6ms)

  ● detects node environment

    expect(received).toBe(expected) // Object.is equality
    
    Expected value to be:
      "undefined"
    Received:
      "object"

       9 |  */
      10 | test('detects node environment', () => {
    > 11 |   expect(typeof document).toBe('undefined');
      12 |   expect(typeof window).toBe('undefined');
      13 | });
      14 | 
      
      at Object.<anonymous>.test (env-test.js:11:27)

Expected behavior

The test should pass, adding the code block is, by documentation, intended to simulate a node environment, where neither window nor document should be defined:

➜  $ ✗ node -v
v8.9.2
➜  $ ✗ node
>
(To exit, press ^C again or type .exit)
> typeof window;
'undefined'
> typeof document;
'undefined'

Link to repl or repo (highly encouraged)

https://repl.it/repls/RoughAcceptableDiscussion

Run npx envinfo --preset jest

Paste the results here:

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

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Binaries:
    Node: 8.9.2 - /usr/local/bin/node
    Yarn: 1.2.1 - /usr/local/bin/yarn
    npm: 5.5.1 - /usr/local/bin/npm
  npmPackages:
    @types/jest: 22.0.1 => 22.0.1
    jest: 22.4.0 => 22.4.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
doquecommented, May 23, 2018

That seems to fix it, thanks! I must have missed where it says ‘use jsdom in this test file’ in the docs, not ‘in this suite’ or ‘in this test’. Could this be explained more clearly in the documentation?

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it...
Read more >
ReferenceError: document is not defined Jest React Testing ...
I am publishing a react component to npm package, I added Webpack, babel, linting, and test to it, initially Webpack builds successfully, tests ......
Read more >
Testing with Node, Jest, and JSDOM - Manning Publications
You can think of JSDOM as an implementation of the browser environment which can run within Node. It implements web standards using pure ......
Read more >
jest referenceerror: document is not defined - You.com | The AI ...
document relates to the DOM (Document Object Model) in a web browser. Node.js, however, is not a browser environment. It is a server...
Read more >
Configuring Jest - API Manual
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through jsdom. If you are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found