javascript - How do I set the timezone in jest config in Create React App? - Stack Overflow

I wrote some tests in my Create React App application that passed locally but failed in CI due to diffe

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';
};

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';
};
Share Improve this question asked Feb 8, 2021 at 20:04 Keith MurgicKeith Murgic 4576 silver badges8 bronze badges 3
  • but failed in CI due to differences in timezone sounds like you wrote bad (brittle) tests. Rather than mucking with the timezone, you should probably fix the tests so they pass independent of timezone. – Adam Jenkins Commented Feb 18, 2021 at 13:21
  • @Adam In this case the utility being tested formatted the time based on the user's current timezone. So it makes the most sense to me to have the test say given that the user is in a certain timezone, this utility should return x, which means mocking the timezone somehow. I also could arguably have mocked out the Date.toLocaleString function that relies on timezone, which was an option I considered, but I preferred to write some tests to make sure my code was properly integrated with that method. – Keith Murgic Commented Feb 22, 2021 at 13:13
  • Pass the timezone in as an argument and then you can mock it however you want. Set the default value for that argument to be navigator.language. – Adam Jenkins Commented Feb 22, 2021 at 13:18
Add a ment  | 

3 Answers 3

Reset to default 3

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

For a cross-platform solution, you can create a .env.test (if not already created) file and add TZ=UTC to the environment variables there.

I'll add this as an answer even though one has already been accepted.

All you're trying to do is mock something, as you've pointed out. All you need to do is just make it a little bit easier to mock.

If you had a function like this:

const formatTime = (time) => new Date(time).toLocaleString()

Then just modify it to look like this:

const formatTime = (time, timezone) => new Date(time).toLocaleString(timezone)

and voila! Testing this bees trivial because now you can just pass in an argument to your function to verify it's functionality. No messing with testing environments or anything. Can't get much easier than testing a pure function.

You don't need to test that toLocaleString uses the default time zone when non is supplied because that's not your code - it's a spec that you are adhering to.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744415401a4573092.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信