2

I have a very simple React/TypeScript application, and I'm trying to learn how to implement testing. I'm using React Testing Library and Jest. It's a very simple product page, and I just want to test that the words "Welcome to our product page" have rendered.

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

I went through all the solutions in similar questions on Stack Overflow, but nothing is working. I tried adding

/**
 * @jest-environment jsdom
*/ 

to the top of the test file, but that just produces a different error message: ReferenceError: global is not defined.

I'd appreciate any help. Below is my code:

ProductPage.test.tsx

import React from "react";
import { render, screen } from "@testing-library/react";
import ProductPage from "../ProductPage";

describe("<ProductPage />", () => {
  test("should display the product page", () => {
    render(<ProductPage />);
    expect(
        screen.getByText(/Welcome to our product page/)
    ).toBeInTheDocument();
  });
});

jest.config.js

module.exports = {              
  roots: ["<rootDir>/src"],          

  transform: {
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.svg$": "<rootDir>/svgTransform.js",
    "^.+\\.css$": "<rootDir>/cssTransform.js"
  },       
  
  setupFilesAfterEnv: [        
    "@testing-library/jest-dom/extend-expect"
  ],    

  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",    

  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};  

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}  

package.json

{
  "name": "product-page",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@apollo/client": "^3.4.17",
    "@babel/core": "^7.16.0",
    "@babel/preset-env": "^7.16.4",
    "@babel/preset-typescript": "^7.16.0",
    "@emotion/react": "^11.6.0",
    "@emotion/styled": "^11.6.0",
    "@mui/material": "^5.1.1",
    "@mui/system": "^5.1.1",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^27.0.2",
    "@types/node": "^16.11.7",
    "@types/react": "^17.0.35",
    "@types/react-dom": "^17.0.11",
    "babel-jest": "^27.3.1",
    "graphql": "^16.0.1",
    "jest": "^27.3.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "ts-jest": "^27.0.7",
    "typescript": "^4.4.4",
    "web-vitals": "^1.0.1"    
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "test:watch": "jest --watch",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "ts-node": "^10.4.0"
  }
}

2 Answers 2

3

In order to use jsdom you need to add

testEnvironment: 'jsdom',

to your jest.config.js

Edit: You might need to add it as a dependency also.

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, I'm new to testing. Do I just add it to anywhere within the jest.config.js file? And for the dev dependencies, do I just add "testEnvironment": "jsdom" under dependencies in the package.json file? I did both of the above, and I'm still getting ` ReferenceError: global is not defined`
You just add it in your jest.config.js, after testRegex for example...
What I meant is that maybe you don't have jsdom in your (dev)dependencies in the node_moldules, in that case you would have to add it as a devDependency npm install --dev jsdom or yarn add jsdom
Thank you. I added it to jest.config and added jsdom as a dev dependency. Still getting ReferenceError: global is not defined :(
Thanks. Installing jsdom did not help, but installing jest-environment-jsdom did. Now my tests actually run. I'm still getting an error, but it's related to something else now.
|
1
  1. Add testEnvironment: 'jsdom' to jest.config.js. This resolved the jsdom error.
  2. Add jest-environment-jsdom as a dev dependency (adding jsdom did not help for whatever reason). This resolved the global error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.