Git Product home page Git Product logo

Comments (8)

nzcodarnoc avatar nzcodarnoc commented on June 12, 2024 4

Try adding this comment block to the top of the file that contains the tests:

/**
 * @jest-environment jsdom
 */

from react-hooks-testing-library.

leejunhui avatar leejunhui commented on June 12, 2024 3

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.

Leif-Frederiksen avatar Leif-Frederiksen commented on June 12, 2024

from react-hooks-testing-library.

leejunhui avatar leejunhui commented on June 12, 2024

Same issue when I use vitest as the testing container to run the useCounter demo test.

image

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.

Angekap avatar Angekap commented on June 12, 2024

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.

ignaciobockl avatar ignaciobockl commented on June 12, 2024

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.

Syrathe avatar Syrathe commented on June 12, 2024

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.

leejunhui avatar leejunhui commented on June 12, 2024

/**

  • @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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.