Consider using the "jsdom" test environment.
See original GitHub issueI 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:
- Created 3 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had a simular issue and solved it by adding the
testEnvironment
in myjest.config.js
like this:Hope it helps!
You should be able to solve this by adding this to the top of any test files that require
document
: