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:

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

Answers:

Answer

You need to have react-test-renderer installed:

yarn add react-test-renderer // or add it to your dev dependencies!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.