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.

Consider using the "jsdom" test environment.

See original GitHub issue

I currently have the following dependencies in packages.json file

  "dependencies": {
    "@types/bcrypt": "^3.0.0",
    "bcrypt": "^5.0.0",
    "dotenv": "^8.2.0",
    "mongodb": "^3.5.9",
    "next": "9.4.4",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  },
  "devDependencies": {
    "@babel/preset-typescript": "^7.10.1",
    "@fullhuman/postcss-purgecss": "^2.3.0",
    "@shelf/jest-mongodb": "^1.1.5",
    "@testing-library/jest-dom": "^5.11.0",
    "@testing-library/react": "^10.4.3",
    "@types/jest": "^26.0.3",
    "@types/mongodb": "^3.5.25",
    "@types/node": "^14.0.14",
    "@types/react": "^16.9.41",
    "@typescript-eslint/eslint-plugin": "^3.4.0",
    "@typescript-eslint/parser": "^3.4.0",
    "autoprefixer": "^9.8.4",
    "jest": "^26.1.0",
    "mongodb-memory-server-core": "^6.6.1",
    "tailwindcss": "^1.4.6",
    "typescript": "^3.9.5"
  }

my jest.config.js has the following

module.exports = {
    preset: '@shelf/jest-mongodb',
    collectCoverageFrom: [
        'components/*.{js,jsx,ts,tsx}',
        'pages/*.{js,jsx,ts,tsx}',
    ],
    setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
    testPathIgnorePatterns: ['/node_modules/', '/.next/'],
    transform: {
        '^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
        '^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
    },
    transformIgnorePatterns: [
        '/node_modules/',
        '^.+\\.module\\.(css|sass|scss)$',
    ],
    moduleNameMapper: {
        '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
    },
}

each time I attempt to run the tests I get the following error

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.
    
    ReferenceError: document is not defined

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
nextglabscommented, Jul 12, 2021

I had a simular issue and solved it by adding the testEnvironment in my jest.config.js like this:

module.exports = {
  testEnvironment: "jsdom",
  // ... other options
}

Hope it helps!

5reactions
helloitsjoecommented, Oct 16, 2020

You should be able to solve this by adding this to the top of any test files that require document:

/**
 * @jest-environment jsdom
 */
Read more comments on GitHub >

github_iconTop Results From Across the Web

Consider using the "jsdom" test environment - Stack Overflow
If your project has a mix of UI and non-UI files, this is often preferable to changing the entire project by setting "testEnvironment":...
Read more >
Consider using the "jsdom" test environment error [Solved]
The error 'Consider using the "jsdom" test environment' occurs when we forget to set the testEnvironment property to jsdom when testing a client-side ......
Read more >
Setup - Testing Library
jsdom is a pure JavaScript implementation of the DOM and browser APIs that runs in node. If you're not using Jest and you...
Read more >
[Solved]-Consider using the "jsdom" test environment-Reactjs
In your package.json , or jest.config.js / jest.config.ts file, change the value of the testEnvironment property to jsdom .
Read more >
Testing with Node, Jest, and JSDOM - Manning Publications
To be able to run your tests in Jest, instead of running your tests within the browser, you can bring browser APIs to...
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