1

I want to write a test for a react component using 'react-datetime' library. The component is using a lot of moment functions which I have to mock with jest one after another. I all the time get errors in the console while running test and keep adding it to jest.mock function. I run into a problem because I get an error like:

TypeError: locale.longDateFormat is not a function

So I add it to jest mock:

jest.mock('moment', () => () => ({
  localeData: () => ({
    longDateFormat: () => '2011–01–30T12:34:56+00:00',
  }),
}));

Then I get an error

TypeError: date.localeData is not a function

which dissapears when added:

localeData: () => '2011–01–30T12:34:56+00:00',

I am not able to add both at the same time as object must not have same keys... How can I combine these two so both errors get fixed? Or maybe there is a way to mock all moment's functions (as in jest.mock I currently have like 20 of them...)

skyboyer
  • 22,209
  • 7
  • 57
  • 64
heisenberg7584
  • 563
  • 2
  • 10
  • 30
  • it's unclear why are you unable to mock both methods at the same time. and anyway you better place mock under `__mocks__/moment.js` as described at [Mock Modules](https://jestjs.io/docs/en/manual-mocks#mocking-node-modules) section to avoid writing the same code for each test. – skyboyer May 28 '19 at 07:11

0 Answers0