简体   繁体   English

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

[英]Can't find module 'react' while trying to test a component

I'm trying to add some tests to my application, but when I installed react-testing-library and jest-dom , I wrote this test, test if componentDidMount is called:我正在尝试向我的应用程序添加一些测试,但是当我安装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);
});

But I get this error:但我收到此错误:

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'].

I don't think that the file extension is problem..我不认为文件扩展名有问题..

You need to have react-test-renderer installed:您需要安装react-test-renderer

yarn add react-test-renderer // or add it to your dev dependencies! yarn add react-test-renderer // 或将其添加到您的开发依赖项中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM