Mocking using Jest
1 min readNov 21, 2020
There are lots of different ways to mock things out using Jest. I have a whole article on mocking out useSelector and useDispatch.
In this post, I’m going to describe a few more ways you can mock things out according to your needs.
- Mocking out a return value for your entire test suite
If your entire test suite needs the same mocked implementation for all tests, it might be cleanest to mock out the function at the top of the spec file.
// Top of your spec file
jest.mock('react-redux', () => ({
useSelector: jest.fn().mockResolvedValue(Y'),
}))
2. Mocking out a named export in your own codebase
// Top of your spec file
import { foo } from '../utils/foo'jest.mock('../utils/foo')// You can either mock an implementation for your entire test suite, or do this for each test case
foo.mockImplementation(() => {/* dummy implementation */})