代码之家  ›  专栏  ›  技术社区  ›  merko

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

  •  0
  • merko  · 技术社区  · 7 年前

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

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

    0 回复  |  直到 7 年前
        1
  •  0
  •   JulienRioux    6 年前

    你需要 react-test-renderer 安装:

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