fkhadra / react-toastify

React notification made easy 🚀 !
https://fkhadra.github.io/react-toastify/introduction
MIT License
12.73k stars 700 forks source link

Cannot mock lib #734

Closed alex-clear closed 2 years ago

alex-clear commented 2 years ago

Hi guys First of all great lib! Thanks I am trying to build tests for my app When I add mock to you lib like:

jest.mock('react-toastify', () => {
  const actual = jest.requireActual('react-toastify')
  Object.assign(actual, {
    toast: jest.fn(),
    ToastContainer: jest.fn(() => <div />),
  })
  return actual
})

I get next error:

 Cannot find module 'react' from '../node_modules/react-toastify/dist/react-toastify.cjs.development.js'

    Require stack:
      /Users/alexander.berdichevsky/Desktop/DEV/node_modules/react-toastify/dist/react-toastify.cjs.development.js
      /Users/alexander.berdichevsky/Desktop/DEV/node_modules/react-toastify/dist/index.js

I did not find any similar issues in web or in your issues Thanks for help "react-toastify": "^8.2.0",

fkhadra commented 2 years ago

Hey @alex-clear, can you try the following instead

jest.mock('react-toastify', () => {
 // needed because your ToastContainer mock use jsx
  const React = require("react") ;
  const actual = jest.requireActual('react-toastify')
  Object.assign(actual, {
    toast: jest.fn(),
    ToastContainer: jest.fn(() => <div />),
  })
  return actual
})
alex-clear commented 2 years ago

Thanks @fkhadra Solved