Search code examples
javascriptunit-testingtimejestjscreate-react-app

How do I set the timezone in jest config in Create React App?


I wrote some tests in my Create React App application that passed locally but failed in CI due to differences in timezone. So I need a way to set the timezone to UTC for all tests in my application. I tried the suggestions on this question, but they didn't work, likely due to Create React App:

Things that I have tried:

  • changing test script from react-scripts test to TZ=UTC && react-scripts test
  • Adding the following snippet to setupTests.js:
module.exports = async () => {
    process.env.TZ = 'UTC';
};

Solution

  • It should be "test": "TZ=UTC react-scripts test", (without &&).

    Note that it's working only when you run it from the terminal (because I'm running sometimes tests from IDE and then you have to change it separate in IDE tests config).