🔍 How to Mock the moment
Library Using Jest
Keywords: Jest mock moment, mock date with Jest, Jest moment.js mock, Jest unit testing moment, mocking npm packages Jest
🧠 TL;DR
If you're using moment
in your JavaScript/TypeScript code and want to mock dates in your Jest tests, you can easily mock moment()
using jest.mock
. Here's the quick version:
jest.mock('moment', () => () => ({
format: () => '2025-03-24T10:00:00Z',
}));
But let’s go beyond the basics. As a senior developer, you want your mocks clean, flexible, and reusable.
🧪 Why Mock moment
in Jest?
When writing unit tests, time-based logic can get flaky. Your code might:
- Generate timestamps
- Format dates/times
- Calculate durations
- Compare time windows
You don’t want your tests to depend on actual time, because that makes them:
- Non-deterministic
- Difficult to reproduce bugs
- Fragile across timezones or daylight saving
👉 So instead of relying on real-time, we mock it.
✅ Basic Example: Mock moment()
to Return a Fixed Date
Let’s say your function looks like this:
import moment from 'moment';
export function getCurrentISODate() {
return moment().format();
}
You want getCurrentISODate()
to always return the same thing in tests.
🧪 Jest Test With Mock:
import { getCurrentISODate } from './date-utils';
jest.mock('moment', () => () => ({
format: () => '2025-03-24T10:00:00Z',
}));
describe('getCurrentISODate', () => {
it('returns mocked ISO date', () => {
expect(getCurrentISODate()).toBe('2025-03-24T10:00:00Z');
});
});
Boom. 🎯 Predictable, testable, and no time travel needed.
🧰 Advanced Mocking: Customize the Return Value
Sometimes you want control over what moment()
returns in each test.
Here’s how to mock with a variable:
import moment from 'moment';
import { getCurrentISODate } from './date-utils';
let mockedDate = '2025-03-24T12:00:00Z';
jest.mock('moment', () => () => ({
format: () => mockedDate,
}));
test('uses custom mock date', () => {
mockedDate = '2025-03-24T18:30:00Z';
expect(getCurrentISODate()).toBe('2025-03-24T18:30:00Z');
});
Now you’re mocking like a pro. 🧑💻
🧙♂️ Want to Mock moment()
to Return a Specific Moment Instance?
Sometimes your app relies on things like moment().isAfter(...)
or moment().add(...)
. In that case, just return a real moment
object seeded with your fake date:
import realMoment from 'moment';
jest.mock('moment', () => {
const mockedMoment = realMoment('2025-03-24T00:00:00Z');
return () => mockedMoment;
});
This way, you can still use add()
, isAfter()
, etc., just frozen in time.
⌛ Alternative: Use jest.useFakeTimers
+ Date
If you’re using moment()
just to get the current time, consider mocking the global Date object instead:
jest.useFakeTimers().setSystemTime(new Date('2025-03-24T00:00:00Z'));
Then moment()
will pick up the frozen time too, because it reads from Date.now()
under the hood.
🔚 Conclusion
Mocking moment
in Jest is simple, but doing it right makes your tests stable and clean.
Quick Recap:
- Use
jest.mock('moment', () => ...)
to fakemoment()
- Return real or partial
moment
objects depending on your use case - Consider
jest.useFakeTimers
for global date mocking