Noo*_*oob 5 testing unit-testing reactjs jestjs react-testing-library
我创建了一些基本测试并遵循 Jests 网站上的入门指南,但 toHaveAttribute 显然不是一个函数
import React from "react";
import { fireEvent, render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { App } from "../App";
test("allows users to add items to their list", () => {
const { getByText, getByLabelText, getByTestId } = render(<App />);
const input = getByLabelText("What needs to be done?");
userEvent.type(getByTestId("email"), "Hello World!")
expect(getByTestId("email")).toHaveAttribute("value", "Hello, World!")
})
Run Code Online (Sandbox Code Playgroud)
类型错误:expect(...).toHaveAttribute 不是函数
10 | const input = getByLabelText("What needs to be done?");
11 | userEvent.type(getByTestId("email"), "Hello World!")
> 12 | expect(getByTestId("email")).toHaveAttribute("value", "Hello, World!")
| ^
13 | })
Run Code Online (Sandbox Code Playgroud)
我完全按照教程进行操作,所以我不确定为什么会发生这种情况。
buz*_*tto 13
该方法toHaveAttribute是jest-dom 的一部分,可以测试 DOM 元素。您需要验证是否在项目中正确设置了它。
安装模块:
npm install --save-dev @testing-library/jest-dom
Run Code Online (Sandbox Code Playgroud)
之后,您可以像推荐的那样在您的笑话设置文件中包含:
// In your own jest-setup.js (or any other name)
import '@testing-library/jest-dom'
// In jest.config.js add (if you haven't already)
setupFilesAfterEnv: ['<rootDir>/jest-setup.js']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3145 次 |
| 最近记录: |