Open alirezahmz opened 2 years ago
When You want to mock a package such as Moment
in Jest
(or any other test library for that matter), You need to implement mock for all the methods the package provides (or more precisely "for all the methods the package provides that Your code uses").
In this particular case, Your code uses moment.localeData()
in addition to moment.format()
, so ... Your mock should also implement that method.
Basically, Your mock should look something like....
jest.mock('moment', () => ({
format: () => '2018-01-30T12:34:56+00:00',
localeData: () => /* should return whatever your test expects it to ... YMMV */
}));
Sometimes mocking packages is a good approach in testing. Sometimes You want to actually use the real package (but preheat it with known values during testing). Your mileage may vary. Hope this helps.
You could use jest.requireActual so you woldn't have to mock the behavior of something that you are probably going to expect to work the same us usual. You could also set jest.setSystemTime to add consistency to tests, doing so could help you with things like testing calendars or similar stuff
i'm using moment-jalaali": "^0.9.4", i get this error when i try to run test : moment is not a function and after google it, i found this code: jest.mock('moment', () => ({ format: () => '2018–01–30T12:34:56+00:00' })); and run again get this error: TypeError: moment.localeData is not a function