Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jest Mocking Moment to same time zone for tests
// To mock globally in all your tests, add to setupTestFrameworkScriptFile in config:
// https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string
jest.mock('moment', () => {
const moment = require.requireActual('moment-timezone');
moment.tz.setDefault('America/Los_Angeles'); // Whatever timezone you want
return moment;
});
@dandigangi

This comment has been minimized.

Copy link

dandigangi commented Dec 19, 2017

Do you have more info on how to add this to your setupTestFrameworkScriptFile?

We're using the built-in located in the node_modules folder for Jest referenced by our Jest config file. cc @RDP07

Is it just adding a JS file to this like so in our Jest config?

"setupFiles": [
      "<rootDir>/config/enzyme.js",
      "<rootDir>/config/testing/moment.js" // <----
    ],
@astrotim

This comment has been minimized.

Copy link

astrotim commented Feb 14, 2018

"setupFiles": [
  "<rootDir>/config/enzyme.js"
],
"setupTestFrameworkScriptFile": "<rootDir>/config/momentMock.js",
@testacode

This comment has been minimized.

Copy link

testacode commented Mar 15, 2018

Hi there! I encounter an issue. I'm getting a TypeError: Cannot read property 'setDefault' of undefined. What I did was just add your snippet to my setupTestFrameworkScriptFile.

@c58

This comment has been minimized.

Copy link

c58 commented Mar 22, 2018

@testacode I have the same issue, and this works for me...

const moment = require.requireActual('moment-timezone');
jest.doMock('moment', () => {
  moment.tz.setDefault('Asia/Yekaterinburg');
  return moment;
});
@nol13

This comment has been minimized.

Copy link

nol13 commented Mar 22, 2018

@ecbrodie

This comment has been minimized.

Copy link

ecbrodie commented Jun 17, 2018

@bbshih this gist is lit!!! Thank you.

@ifiokjr

This comment has been minimized.

Copy link

ifiokjr commented Jun 18, 2018

Thanks @c58 your update fixed it for me! It seems when updating to Jest 23 the original method stops working and you have to use doMock.

Anyone know the reason?

@duranmla

This comment has been minimized.

Copy link

duranmla commented Sep 6, 2018

I just use it as @c58 says and works perfectly, thank you

@joeheyming

This comment has been minimized.

Copy link

joeheyming commented Oct 3, 2018

I had an issue related to this and wanted to share my problem:
In my jest.config, I had
'^moment': '/node_modules/moment'

But it was too permissive and matched moment-timezone as well. Needless to say, great confusion occurred.
The correct config is and exact match regular expression:
'^moment$': '/node_modules/moment'

Hope this helps others.

@KhalilZaidoun

This comment has been minimized.

Copy link

KhalilZaidoun commented Nov 6, 2018

Using jest.doMock instead of jest.mock has helped me.

// To mock globally in all your tests, add to setupTestFrameworkScriptFile in config:
// https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string
jest.doMock('moment', () => {
  const moment = require.requireActual('moment-timezone');
  moment.tz.setDefault('America/Los_Angeles'); // Whatever timezone you want
  return moment;
});
@rosinghal

This comment has been minimized.

Copy link

rosinghal commented Jan 18, 2019

Nice one. Thanks.

@vmakarevich

This comment has been minimized.

Copy link

vmakarevich commented Jan 22, 2019

Is it right solution to use separate package for setting timezone? I don't understand from where moment gets timezone, tried to mock Date.prototype.getTimezoneOffset , but looks like moment don't uses it. Also founded that for negative offset tests not fails, only if my offset is more than 0 tests fails.

@valoricDe

This comment has been minimized.

Copy link

valoricDe commented Apr 30, 2019

I had to use require instead of requireActual otherwise I would get moment.tz === undefined

const moment = require('moment-timezone');
jest.doMock('moment', () => {
  moment.tz.setDefault('Asia/Yekaterinburg');
  return moment;
});
@soorajshankar

This comment has been minimized.

Copy link

soorajshankar commented Jun 16, 2019

Add the following before describe in your test
const moment = require.requireActual('moment-timezone').tz.setDefault('America/Los_Angeles');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.