Jest – Mock Hardcoded Array

I want to mock numbers function that exports list of numbers:

// utils.js
export const numbers = () => [1, 2, 3, 4];
export const letters = () => ['x', 'y', 'z'];
// ...and so on

and mock:

// utils.spec.js
jest.mock('utils.js', () => ({
  ...jest.requireActual('utils.js'),
  numbers: jest.fn().mockReturnValue([5, 6, 7]),
})); 

When I run my test case, numbers are still [1, 2, 3, 4]?

Looks like I didn’t mock anything?

44 thoughts on “Jest – Mock Hardcoded Array”

Leave a Comment