reactjs - 如果我不导入它,为什么我可以使用来自“@testing-library/jest-dom”的expect expect().toBeInTheDocument?
问题描述
我使用“create React App”创建了一个 React.js 应用程序,并且我正在学习运行测试。
expect().toBeInTheDocument
如果我不导入,为什么在下面的代码中可以使用@testing-library/jest-dom
?
import {render, screen} from "@testing-library/react";
import Greeting from "./Greeting";
test("renders Hello World as a text", () => {
render(<Greeting/>);
const helloWorld = screen.getByText('Hello World');
expect(helloWorld).toBeInTheDocument
});
解决方案
当您运行create react app
它时,它会在您的目录中创建一个名为setup-test.js
(或者.ts
如果您使用 TypeScript)的文件,该文件会在那里src
导入 @testing-library/jest-dom
此文件由 jestcreate-react-app
在运行测试之前加载(配置为这样做)(我假设使用配置setupFiles
)
推荐阅读
- ruby-on-rails - ActiveAdmin:使用过滤器和 AA 约定,但完全控制资源加载和验证
- java - 使用 Gson 将 Java 对象转换为 javax.ws.rs 中的 Json
- javascript - 如何在 javascript/jquery 中选择 DOM 创建的 li?
- c - 如何初始化指向外部变量的全局指针?
- c# - 单元测试 MiddleWare,如何在 .NET Core 3.1 中将 HttpRequest 添加到 HttpContext
- javascript - 如何解决错误消息:类型“未知”不可分配给类型“BlobPart”
- math - 如何在学校计算器上计算像 SHA1 这样的哈希?
- php - 如何在 php 中以 cookie 值发送正确的翻译
- rust - 多个可变和不可变借用的最佳实践是什么?
- git - 正确使用 git check-mailmap