Skip to content

Instantly share code, notes, and snippets.

@kevgathuku
Created September 4, 2018 07:36
Show Gist options
  • Select an option

  • Save kevgathuku/0080cec18b4d10bf48beaf2cdd95ee8b to your computer and use it in GitHub Desktop.

Select an option

Save kevgathuku/0080cec18b4d10bf48beaf2cdd95ee8b to your computer and use it in GitHub Desktop.

Mocking an imported function to return different values for different tests

In the top level:

import getVisibleDays from 'react-dates/lib/utils/getVisibleDays';

jest.mock('react-dates/lib/utils/getVisibleDays', () => {
  const moment = require('moment');
  return jest.fn().mockReturnValue({
    '2018-01': [
      moment('2017-12-31'),
    ],
  });
});

In an individual test where you want to return a different value:

it('uses different values', () => {
  getVisibleDays.mockImplementation(() => {
    const moment = require('moment');
    return {
      '2018-01': [
        moment('2018-01-04'),
      ],
    };
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment