如果我不导入它,为什么我可以从"@testing-library/jest-dom"中使用期望期望().toBeInTheDocument?



我用"create React App"创建一个React.js应用程序。我正在学习做测试

如果我不导入@testing-library/jest-dom,为什么在下面的代码中我可以使用expect().toBeInTheDocument?

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时,它会在你的src目录中创建一个名为setup-test.js(或.ts,如果你使用TypeScript)的文件,在那里导入@testing-library/jest-dom

在运行测试(我假设使用配置setupFiles)之前,该文件由jest加载(create-react-app配置它这样做)

最新更新