1

我正在开发一个反应应用程序,突然添加两个测试后我得到一个错误,即

ReferenceError: expect is not defined
./node_modules/@testing-library/jest-dom/dist/extend-expect.js
node_modules/@testing-library/jest-dom/dist/extend-expect.js:9

我试图实施我在互联网上找到的解决方案,但没有任何效果。这是我的两个测试文件,第一个,

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

describe("<Index/>", () => {
  it("checking render", () => {
    render(<Index />);
  });
});
it("checking text", () => {
  const { getByTestId } = render(<Index />);
  const { getByText } = within(getByTestId("durbin"));
  expect(getByText("DurBinLab-Test")).toBeInTheDocument();
});

第二个是,

import React from "react";
import { render, fireEvent, within, screen } from "@testing-library/react";
import Login from "./Login";
import userEvent from "@testing-library/user-event";
describe("<Login/>", () => {
  it("checking render", () => {
    render(<Login />);
  });
});

it("text check", () => {
  const { getByTestId } = render(<Login />);
  const { getByText } = within(getByTestId("login"));
  expect(getByText("Login")).toBeInTheDocument();
});

describe("Input Field Check", () => {
  test("render email input", () => {
    render(<Login />);

    const inputEl = screen.getByTestId("email-input");
     expect(inputEl).toBeInTheDocument();
  });

  test("pass valid email to test email input field", () => {
    render(<Login />);

    const inputEl = screen.getByTestId("email-input");
    userEvent.type(inputEl, "test@mail.com");

});

最后是我的 package.json

{
  "name": "user-manage",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@emotion/react": "^11.4.1",
    "@emotion/styled": "^11.3.0",
    "@mui/icons-material": "^5.0.0",
    "@mui/material": "^5.0.0",
    "@testing-library/react": "^12.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.5",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "redux": "^4.1.1",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.3.0",
    "uuid": "^3.4.0",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "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": {
    "@testing-library/jest-dom": "^5.14.1"
  }
}

这就是我的 JSON 文件的样子。我试图编辑 setuptest.js 文件。但问题还是一样。

4

0 回答 0