Yes you can but will need to use spies. For example if you are using jest
you can you use jest.spyOn with mockImplementationOnce(), It should also work with other spying
libraries.
Using Example:
describe("React useEffect", () => {
let useEffect, wrapper
let mockUseEffect = () => {
useEffect.mockImplementationOnce(f => f())
}
beforeEach(() => {
useEffect = jest.spyOn(React, "useEffect")
mockUseEffect()
wrapper = shallow(<SomeComponent />)
})
it("your test", () => {
// using wrapper
})
}
Here is a nice detailed article by "Will Ockelmann-Wagner" and I added a codeSandbox working example.
Although it works, I'd encourage you to not use shallow
rendering and start using react-testing-library
(called @testing-library/react nowadays), Its well-documented, lightweight Testing solution, and I'd say its the the "closest" test you can get to real world scenarios. It comes already with CRA by default.
Here a basic codeSandbox example and Here is more examples from React Docs.
Also, here is a great Video by Kent C. Dodds that addressing the differences between shallow
rendering and the render
approach from react testing library.
mount
(especially if you are rendering a connected component), I still think it is the most straight forward way to test its behaviour – wentjun