Comments (8)
Try adding this comment block to the top of the file that contains the tests:
/**
* @jest-environment jsdom
*/
from react-hooks-testing-library.
After installed the jest-environment-jsdom
as devDependency and added the
/**
* @jest-environment jsdom
*/
at the top of the test file, the error was gone!
from react-hooks-testing-library.
from react-hooks-testing-library.
Same issue when I use vitest as the testing container to run the useCounter
demo test.
And my package.json as below:
{
"name": "hooks-test",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "bunx --bun vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.5",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vitejs/plugin-react": "^4.0.3",
"eslint": "^8.45.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"jest": "^29.7.0",
"react-test-renderer": "^18.2.0",
"typescript": "^5.0.2",
"vite": "^4.4.5",
"vitest": "^0.34.4"
}
}
from react-hooks-testing-library.
After installed the
jest-environment-jsdom
as devDependency and added the/** * @jest-environment jsdom */
at the top of the test file, the error was gone!
This worked for me
from react-hooks-testing-library.
After installed the
jest-environment-jsdom
as devDependency and added the/** * @jest-environment jsdom */
at the top of the test file, the error was gone!
This worked for me
from react-hooks-testing-library.
Try adding this comment block to the top of the file that contains the tests:
/** * @jest-environment jsdom */
It also worked for me. But why?
from react-hooks-testing-library.
/**
- @jest-environment jsdom
*/
it specifies the environment in which the tests should run. In Jest, the testing environment can be Node.js, jsdom, or Jest's default pseudo-browser environment. The @jest-environment jsdom comment tells Jest to use jsdom as the testing environment.
jsdom is a library that emulates a web browser environment, allowing you to run tests within a Node.js context without the need for an actual browser. This is particularly useful for testing web applications' DOM manipulations and browser APIs, as it can simulate a full browser environment, including the Document Object Model (DOM) and the browser window object (window).
In short, when you see @jest-environment jsdom at the top of a test file, it means that all the tests will be run in a simulated browser environment rather than in a Node.js environment. This is very helpful for testing code that relies on browser-specific features.
from react-hooks-testing-library.
Related Issues (20)
- Critical dependency: the request of a dependency is an expression HOT 5
- renderHook() does not clean useRef in custom hooks HOT 2
- useHistory in react-router-dom v5 HOT 2
- renderHook does not work with React Magnetic DI wrapper HOT 1
- Warning: ReactDOM.render is no longer supported in React 18. HOT 1
- Why does the UI not update when calling a hook method? HOT 1
- Error when installing with React 18.x HOT 1
- Migration to react 18 HOT 1
- Testing custom hooks that needs the DOM
- Cannot test async useCallback in custom hook HOT 4
- `restoreConsole` is not a function error if a test fails `beforeEach()` HOT 2
- Cannot test hook raises an error HOT 1
- jsdom environment issues HOT 1
- [renderHook] rerender with new props, new props are not consumed HOT 9
- Lib incompatible with [email protected] version HOT 3
- Stateful context not updating properly HOT 3
- type of `wrapper` might be wrong HOT 1
- `renderHook` result with `vitest` and `react@18` always returns `null` HOT 2
- Update @types/react package to version 18 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hooks-testing-library.