尝试测试组件时找不到模块“反应”

时间:2019-05-04 16:20:31

标签: javascript reactjs testing react-testing-library

我正在尝试向应用程序中添加一些测试,但是当我安装react-testing-libraryjest-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'].

我不认为文件扩展名有问题。

1 个答案:

答案 0 :(得分:0)

您需要安装react-test-renderer

yarn add react-test-renderer //或将其添加到您的开发依赖项中!