121
✗ npx jest --version
24.5.0

Got a set of jest tests that are timezone sensitive. We typically run them with an npm script: "jest": "TZ=utc jest"

With the TZ set to utc I get values like this in snapshots:

modificationDate="2019-01-08T00:00:00.000Z" 

Without it I get:

modificationDate="2019-01-08T08:00:00.000Z"

Is there a way to set that in my jest config so I can run npx jest at the command line without having to go through the NPM script? There's nothing in config docs about this.

I tried adding these two to my jest.config.js. Neither one worked:

  TZ: 'utc',

  globals: {
    TZ: 'utc',
  },

Sure, it seems trivial to work around but I'm surprised Jest doesn't have a way to configure this for tests.

8 Answers 8

195

This does not work on windows prior to node 16.2.0 (and prior to 17.0.1 in node 17) - see https://github.com/nodejs/node/issues/4230


The problem with process.env.TZ = 'UTC'; is, that if something runs before this line and uses Date, the value will be cached in Date. Therefore process.env is in general not suitable for setting the timezone. See https://github.com/nodejs/node/issues/3449

So a better way is to use an actual env variable, but for tests this will work:

1. Add this to your package.json

  "jest": {
     ...
     // depending on your paths it can also be './global-setup.js' 
    "globalSetup": "../global-setup.js"
  }
}

2. Put this file besides package.json as global-setup.js

module.exports = async () => {
    process.env.TZ = 'UTC';
};

3. Optional: Add a test that ensures UTC execution

describe('Timezones', () => {
    it('should always be UTC', () => {
        expect(new Date().getTimezoneOffset()).toBe(0);
    });
});

The normal setupFiles did not work for me, since they run too late (jest: ^23.5.0). So it is mandatory to use the globalSetup file.

18
  • 2
    @OliverWatkins, can you verify whether global-setup.js is called or not by adding some console logs?
    – CaKa
    Jul 30, 2019 at 9:50
  • 3
    I tried on Jest 24.8.0 . I put consoles everywhere, and they all get called (ie. also inside the asynch - the line process.env.TZ = 'UTC';). My timezoneoffset is -120, and not zero. Jul 30, 2019 at 12:23
  • 4
    @Can It does not work for me too. Logging process.env.TZ gives me UTC. What's your OS? Mine is Win10. Aug 22, 2019 at 15:53
  • 2
    This solution worked great on my Mac but doesn't seem to work on my Windows 10 machine. My global-setup.js is definitely being called and has ``` process.env.TZ = 'UTC'; console.log('JEST SETUP TZ OFFSET: ' + new Date().getTimezoneOffset()) ``` My test has ``` console.log('IN TEST TZ OFFSET' + new Date().getTimezoneOffset()); console.log('IN TEST TZ' + process.env.TZ); expect(new Date().getTimezoneOffset()).toBe(0); ``` The output of this is ` JEST SETUP TZ OFFSET: 0 IN TEST TZ OFFSET-60 IN TEST TZ UTC Expected: 0 Received: -60 ``` Feb 21, 2020 at 17:20
  • 2
    @JoseSolorzano would you mind testing if cross-env would work? "test": "cross-env TZ=\"UTC\" npm run jest-task",
    – CaKa
    Mar 10, 2020 at 9:40
62

If you are running tests with npm scripts, ie: npm run test, you can pass in the timezone like so:

  "scripts": {
    "test": "TZ=UTC jest"
  },

I also personally feel that this (vs the process.env methods) is cleaner and easier to identify the timezone when debugging issues on remote CI servers.

9
  • 1
    and if I'm running via command line without npm scripts I have to remember to do that every time -- the accepted answer is better
    – jcollum
    May 6, 2020 at 16:45
  • 10
    True. Though I would recommend moving towards using npm scripts. It's easier to keep track of how you and others should run and maintain your application(s). IE: npm run test will run the tests for every node project that I have. You don't have to do a thorough reading of the README (that's hopefully up to date) to know what's expected.or what quirks that app has.
    – KFunk
    May 6, 2020 at 20:18
  • 15
    Disagree. You should only be running your automated tests with one command, preferably one that you and your CI control, like a npm script command.
    – KFunk
    Oct 8, 2020 at 20:28
  • 1
    some of the time you don't want to alter that script command in order to run a subset of tests, making the accepted answer better
    – jcollum
    Feb 4, 2021 at 17:27
  • 2
    This solution doesn't work on Windows. There's another way that does, but then your project is OS dependent.
    – bbsimonbb
    Aug 8, 2022 at 15:02
33

I just run into the same issue and I was able to resolve it by adding process.env.TZ = 'your/timezone'; to my jest.config.js.

Maybe this helps in your case as well :)

process.env.TZ = 'UTC';

module.exports = {
  ...
};
5
  • 2
    This is sadly wrong. See github.com/nodejs/node/issues/3449 "Closing, setting process.env.TZ is not the right way to set the default timezone." Verify with ` expect(new Date().getTimezoneOffset()).toBe(0);`
    – CaKa
    May 26, 2019 at 17:15
  • @CanK. this is weird... I just updated to angular 8 and changed a few dependencies and now my tests are not working anymore... I will let you know if i find another solution.
    – thegnuu
    Jun 6, 2019 at 13:30
  • @thegnuu yes I did, I'll put it into an answer due to the contents.
    – CaKa
    Jun 6, 2019 at 17:33
  • @CanK. I realised that in my case it was just because of a change in the config path that my solution stopped working, but your solution is working as well so I switched to this one, seems to be the better way to set the timezone! Thank you!
    – thegnuu
    Jun 7, 2019 at 12:23
  • This should be the accepted answer. Especially for those of us using monorepos, by far the easiest solution Nov 4, 2021 at 20:49
15

Up until recently I used the following to mock being in a different timezone:

  beforeEach(() => {
    // Temporarily allow us to alter timezone calculation for testing
    /*eslint no-extend-native: "off"*/
    Date.prototype.getTimezoneOffset = jest.fn(() => 73);
  });

  afterEach(() => {
    jest.resetAllMocks();
  });

This didn't place the testing code in a particular timezone, but made sure that any timezone offset calculations were correctly made. For example:

new Date("2010-10-01") would be 73 minutes earlier than new Date("2010-10-01T00:00:00"), with the former being equivalent to new Date("2010-10-01T00:00:00Z") (UTC timezone) and the latter being in the 'local timezone'

I say "up until recently" because a recent update to date-fns seems to no longer work

2
  • I'm using luxon and this code works properly. I imagine it depends on whether the lib relies on getTimezoneOffset to know the TZ difference
    – kiril
    Apr 9, 2022 at 7:50
  • 1
    I should add that I now make use of npmjs.com/package/timezone-mock when testing code in a specific timezone
    – Shiraz
    Apr 24, 2022 at 17:32
8

Update: This doesn't produce deterministic results if you want to dynamically change the TZ but it will work if you only want one TZ. It could be an alternative to specifying at the script level, but I think that would be the better answer here.

The issue is that by setting process.env.TZ at runtime, it will bleed across tests creating non-deterministic behavior (side effects) during a regular Jest test run. It may work if you use --runInBand which runs tests serially but I wouldn't count on it.

I also found an old archived issue about dynamic timezones in Node and it looks like dynamically adjusting it won't work in general.

Instead I will probably end up with multiple scripts that each set TZ before launching jest.


For my use case, I actually wanted to run tests under different timezones for specific date-based edge cases. Sometimes users run into timezone-based bugs and we want to cover that easily within our test suites.

We run all tests in the project by default using one of the proposed answers here, by setting TZ=UTC in the npm script (e.g. TZ=UTC npm run jest. This runs all tests under the UTC timezone.

Then, we leverage the testEnvironment configuration which can be set at the test suite-level using the JSDoc pragma @jest-environment. Using this custom test environment, we can then read the suite's desired timezone using a "custom docblock pragma" like @timezone. This enables timezone customization per-test-suite which is not as ideal as per-test but good enough for our purposes.

jsdom-with-timezone.js

const JSDOMEnvironment = require('jest-environment-jsdom');

/**
 * Timezone-aware jsdom Jest environment. Supports `@timezone` JSDoc 
 * pragma within test suites to set timezone.
 *
 * You'd make another copy of this extending the Node environment, 
 * if needed for Node server environment-based tests.
 */
module.exports = class TimezoneAwareJSDOMEnvironment extends JSDOMEnvironment 
{
  constructor(config, context) {

    // Allow test suites to change timezone, even if TZ is passed in a script.
    // Falls back to existing TZ environment variable or UTC if no timezone is specified.
    // IMPORTANT: This must happen before super(config) is called, otherwise
    // it doesn't work.
    process.env.TZ = context.docblockPragmas.timezone || process.env.TZ || 'UTC';

    super(config);
  }
};

tz-eastern.test.js

/**
 * @timezone America/New_York
 */

describe('timezone: eastern', () => {
  it('should be America/New_York timezone', () => {
    expect(process.env.TZ).toBe('America/New_York');
    expect(new Date().getTimezoneOffset()).toBe(300 /* 5 hours */);
  });
});

jest.config.js

module.exports = {
  "testEnvironment": "<rootDir>/jsdom-with-timezone.js"
}

Using this with jest.useFakeTimers('modern'); and jest.setSystemTime() is sufficient for more robust date testing so I thought I'd share this approach for others to benefit from! Since the pragma handling is custom, you could customize this any way you like for your use case.

Sources:

2
  • Actually, following up (and I edited the answer). This produces non-deterministic behavior when running Jest normally (i.e. with multiple suites, in parallel). The problem is that process.env.TZ bleeds across tests since it is not part of the sandbox. It looks like Node.js doesn't support dynamic TZ at runtime :( github.com/nodejs/node-v0.x-archive/issues/3286
    – kamranicus
    Jan 7, 2021 at 19:58
  • This is great, thank you very much! Just two small notes: --- 1. I also had to pass context to super to make it work - super(config, context); --- 2. expect(new Date().getTimezoneOffset()).toBe(300) will not work always work in timezones with DTS time shifts. To fix this, you can fix the date for which you compare offset, for example: expect(new Date(1683716277208).getTimezoneOffset()).toBe(240); May 10 at 11:15
0

I got the same problem.

at first my jest config was json: jest.config.json I changed this to jest.config.js

This is the file where also all the mapping happens so the timezone has te be set in this file.

Since i couldn't use commonJs anymore my config file looks like this:

process.env.TZ = 'GMT';

export default {
 ... config ...
}
0

EDIT: while the test seemed to work when executed alone, it does not work in general.


from a mix of answers, this is the way I came up with, and the tests to ensure that Timezone change is actually working. I hope this helps someone !

first create a global setup file named 'jest.global-setup.js' (you can name it anything you like) with this content:

module.exports = async () => {
  function changeTimezone(tz) {
    if (tz) {
      process.env.TZ = tz;
    } else {
      delete process.env.TZ;
    }
  }

  if (typeof global !== 'undefined') {
    global.changeTimezone = changeTimezone;
  }

  if (typeof globalThis !== 'undefined') {
    globalThis.changeTimezone = changeTimezone;
  }
};

then add it to your jest config file:

module.exports = {
  // your other configs ...
  globalSetup: '<rootDir>/jest.global-setup.js',
};

now in your tests:

// please notice that I use typescript here,
// you don't have to

const timezones = {
  UTC: 'UTC',
  Afghanistan: 'Asia/Kabul',
};

function getTimezoneIdentity(tz: string) {
  if (tz === timezones.UTC) {
    return 'Coordinated Universal Time';
  }
  if (tz === timezones.Afghanistan) {
    return 'Afghanistan Time';
  }

  return 'time_zone_identity_not_found';
}

const tests = (timezone: string) =>
  describe('timezone is set to ' + timezone, () => {
    beforeAll(() => {
      // @ts-ignore
      changeTimezone(timezone);
    });

    afterAll(() => {
      // @ts-ignore
      changeTimezone('');
    });

    it('timezone change works', () => {
      expect(new Date().toString()).toContain(getTimezoneIdentity(timezone));
    });

    // Your other tests here 
  });

describe('my tests work properly over different timezones', () => {
  for (const tz in timezones) {
    tests(timezones[tz as keyof typeof timezones]);
  }
});
-2

If you are using react scripts you can set timezone like this in your package.json:

"scripts": {        
    "test": "TZ=UTC react-scripts test",        
},
1
  • 1
    from the question: Is there a way to set that in my jest config so I can run npx jest at the command line without having to go through the NPM script? -- this answer does not answer that question
    – jcollum
    Feb 13 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.