131
rated 0 times
[
135]
[
4]
/ answers: 1 / hits: 7286
/ 5 Years ago, tue, august 27, 2019, 12:00:00
I am trying to mock moment library's format function using jest. I have following code in my test file.
app.spec.js:
jest.mock('moment', () => {
const moment = () => ({
format: () => mockedTime
});
moment.tz = {
setDefault: () => {}
};
moment.tz.setDefault('Asia/Singapore');
return moment;
});
app.js:
moment.tz.setDefault(TIMEZONE);
moment().format('YYYYMMDD');
it is generating following output:
- date: 20190825, // mocked date
- date: 20190827, // result value
the expected output should be:
- date: 20190825, // mocked date
- date: 20190825, // result value
Can anyone help me point out what's wrong with the code?
Thanks.
More From » unit-testing