admin管理员组文章数量:1136174
I'm unable to mock moment()
or moment().format
functions. I have states where, currentDateMoment
and currentDateFormatted
are getting set as below.
currentDateMoment: moment() //2019-04-23T17:45:26.339Z
currentDateFormatted: moment().format('MM-DD-YYYY').valueOf() //"04-23-2019"
Trying to mock both moment()
and moment().format
in my snapshot tests to return a particular date, but was unable to. Tried below.
jest.mock('moment', () => () => '2018–01–30T12:34:56+00:00');
jest.mock('moment', () => ({
constructor: () => '2018–01–30T12:34:56+00:00'
}));
jest.mock('moment', () => () => ({ format: () => '01–30-2018' }));
I'm unable to mock moment()
or moment().format
functions. I have states where, currentDateMoment
and currentDateFormatted
are getting set as below.
currentDateMoment: moment() //2019-04-23T17:45:26.339Z
currentDateFormatted: moment().format('MM-DD-YYYY').valueOf() //"04-23-2019"
Trying to mock both moment()
and moment().format
in my snapshot tests to return a particular date, but was unable to. Tried below.
jest.mock('moment', () => () => '2018–01–30T12:34:56+00:00');
jest.mock('moment', () => ({
constructor: () => '2018–01–30T12:34:56+00:00'
}));
jest.mock('moment', () => () => ({ format: () => '01–30-2018' }));
Share
Improve this question
edited Apr 29, 2021 at 11:58
jonrsharpe
122k30 gold badges264 silver badges472 bronze badges
asked Apr 24, 2019 at 21:26
Zin YackvaaZin Yackvaa
9051 gold badge7 silver badges15 bronze badges
7
- This seems like a strange thing to be mocking, why do you need to inject a particular date to perform your testing? – Jake Holzinger Commented Apr 24, 2019 at 21:31
- 2 I have a snapshot test and need to mock moment() to return a certain date to not have it fail the next day... – Zin Yackvaa Commented Apr 24, 2019 at 21:35
- 2 Possible duplicate of How to mock moment.utc() for unit tests? – VincenzoC Commented Apr 24, 2019 at 22:30
- I am also having the same issue. Any update? – AJ007 Commented Jul 4, 2019 at 7:21
- Similar issue in the code base. Do we have a fix for this yet? – koustubh Commented Sep 19, 2019 at 12:29
9 Answers
Reset to default 77The easiest way to mock moment() and any function that it uses (i.e. .day()
, .format()
) is to change the Date
that moment()
uses under the hood
Add the snippet below inside your test file
Date.now = jest.fn(() => new Date("2020-05-13T12:33:37.000Z"));
This makes it so anytime moment()
is called in your tests, moment()
thinks that today is Wednesday, May 13th 2020
You can mock Moment to return a specific date, then format
don't have to be mocked.
jest.mock('moment', () => {
return () => jest.requireActual('moment')('2020-01-01T00:00:00.000Z');
});
By doing so, any call to Moment()
will always return a moment object with date set to 2020-01-01 00:00:00
Here is an example with a function that return the date of tomorrow and the test for this function.
const moment = require('moment');
const tomorrow = () => {
const now = moment();
return now.add(1, 'days');
};
describe('tomorrow', () => {
it('should return the next day in a specific format', () => {
const date = tomorrow().format('YYYY-MM-DD');
expect(date).toEqual('2020-01-02');
});
});
Here is the solution:
index.ts
:
import moment from 'moment';
export function main() {
return {
currentDateMoment: moment().format(),
currentDateFormatted: moment()
.format('MM-DD-YYYY')
.valueOf()
};
}
index.spec.ts
:
import { main } from './';
import moment from 'moment';
jest.mock('moment', () => {
const mMoment = {
format: jest.fn().mockReturnThis(),
valueOf: jest.fn()
};
return jest.fn(() => mMoment);
});
describe('main', () => {
test('should mock moment() and moment().format() correctly ', () => {
(moment().format as jest.MockedFunction<any>)
.mockReturnValueOnce('2018–01–30T12:34:56+00:00')
.mockReturnValueOnce('01–30-2018');
expect(jest.isMockFunction(moment)).toBeTruthy();
expect(jest.isMockFunction(moment().format)).toBeTruthy();
const actualValue = main();
expect(actualValue).toEqual({ currentDateMoment: '2018–01–30T12:34:56+00:00', currentDateFormatted: '01–30-2018' });
});
});
Unit test result with 100% coverage:
PASS src/stackoverflow/55838798/index.spec.ts
main
✓ should mock moment() and moment().format() correctly (7ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.795s, estimated 8s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/55838798
mockdate works for me
import mockDate from "mockdate";
test('Should add some', () => {
mockDate.set(new Date('2/20/2020'));
const action = addSome();
expect(action).toEqual({
createdAt: moment()
});
mockDate.reset();
})
The other answers tell you how to mock moment, but you don't need to mock moment to test that code. You probably shouldn't, in fact; it's a complicated third party interface you don't own and mocking it couples your tests to it, you end up testing implementation rather than behaviour.
Rather than calling moment()
without any arguments, call it with the current date (per https://momentjscom.readthedocs.io/en/latest/moment/01-parsing/01-now/ moment()
is basically moment(new Date())
). Then that current date comes from a function you do own:
const { getCurrentDate } = require('path/to/utils');
export const createSomething =() => ({
currentDateMoment: moment(getCurrentDate()),
currentDateFormatted: moment(getCurrentDate()).format('MM-DD-YYYY'),
});
so you can trivially mock that out in the tests:
const { createSomething } = require('path/to/impl');
const { getCurrentDate } = require('path/to/utils');
jest.mock('path/to/utils');
it('formats the data correctly', () => {
getCurrentDate.mockReturnValue(new Date(2021, 3, 29));
const { currentDateFormatted } = createSomething();
expect(currentDateFormatted).toEqual('2021-04-29');
});
Now the test doesn't refer to moment, which has become an implementation detail, at all. If there's a breaking future change to the moment API you'll find out about it, because your tests will fail; they'd misleadingly pass if it was mocked. If you want to switch to a different library you can do so, confident that the tests mean the behaviour is still correct (here's an example doing the same thing with DayJS).
TL;DR Found testing moment with jest and it really helped me with my solution.
Context
I needed to mock format with the moment-timezone so here is my solution and ran into issues where moment
, format
or other methods within the moment
function were not defined.
Solution
// mock the module using automock from jest
jest.mock('moment-timezone', () => {
// mocking a module requires the function definitions, to only partially mock, use requireActual
const moment = jest.requireActual('moment-timezone');
// create a momentInstance
const momentInstance = moment();
// Override the format function with some mockedTime
jest.spyOn(momentInstance, 'format').mockImplementation(() => mockedTime);
function fakeMoment() {
return momentInstance;
}
Object.assign(fakeMoment, moment);
return fakeMoment;
});
To Mock Moment().format()
,startOf()
, isValid()
or isAfter()
etc. Can refer below example.
jest.mock('moment', () => {
const momentParams = {
format: jest.fn(() => '10/04/2020'),
startOf: jest.fn().mockReturnThis(),
isAfter: jest.fn().mockReturnValue(true),
isValid: jest.fn().mockReturnValue(true)
};
const fn = jest.fn(newMoment => {
momentParams.format = jest.fn(() => newMoment);
return momentParams;
});
return fn;
});
And last you can write a test case like this. eg.
test('should returned mocked value for isAfter()', async () => {
jest.spyOn(moment(), 'isAfter').mockReturnValue(false);
const response = moment().isAfter();
expect(response).toBe(false)
})
I was still getting some errors because I was using moment-timezone, too. So, here's what I did to fix that:
let diffMins = updateThreshold + 1;
jest.mock('moment', () => {
const mMoment = {
diff: jest.fn(() => diffMins),
};
const fn = jest.fn(() => mMoment);
fn.version = '2.24';
fn.tz = jest.fn();
fn.fn = jest.fn();
return fn;
});
I have followed above post and have some errors because only mock some functions instead of moment object time.
Changed a little bit as below
jest.mock('moment-timezone', () => {
const moment = require('moment');
// Mock to the date you want
return moment.utc('2023-01-01 08:30:40')
}
Follow this config, the test function worked as expectation and don't have the error: unix method is null, format method is undefined etc
本文标签: javascriptMocking moment() and moment()format using jestStack Overflow
版权声明:本文标题:javascript - Mocking moment() and moment().format using jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736954535a1957546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论