我正在尝试向我的应用程序添加一些测试,但是当我安装
react-testing-library
和
jest-dom
,我写了这个测试,测试是否
componentDidMount
被称为:
import { render } from "react-testing-library";
import { QuestionContainer } from "../containers/QuestionContainer";
it("should call componentDidMount", () => {
const cDM = jest.spyOn(QuestionContainer.prototype, "componentDidMount");
render(QuestionContainer);
expect(cDM).toHaveBeenCalled(1);
});
但我有一个错误:
Cannot find module 'react' from 'index.js'
However, Jest was able to find:
'../containers/QuestionContainer.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].
我认为文件扩展名不是问题。。