Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a timezone in my Jest config?

Tags:

reactjs

jestjs

✗ 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.

like image 513
jcollum Avatar asked May 22 '19 16:05

jcollum


People also ask

How do I set timezone in node server?

thejh is right, you cannot change the timezone. Use a JS time library (like moment. js) and add / subtract hours instead. The easiest and the correct way to do this is to simply change your system's time zone.

How do I get timezone offset?

Definition and UsagegetTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

Where do I put Jest configuration?

According to documentation, you can use config. testPathDirs : https://facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string and you can call jest --config=<config-file> . Unfortunately the documentation doesn't include any description of how the configuration file should look like.

What is watch mode in Jest?

jest -t name-of-spec. Run watch mode: jest --watch #runs jest -o by default. jest --watchAll #runs all tests. Watch mode also enables to specify the name or path to a file to focus on a specific set of tests.


2 Answers

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.

like image 139
Can Avatar answered Oct 10 '22 14:10

Can


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.

like image 45
KFunk Avatar answered Oct 10 '22 14:10

KFunk