-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting process.env.TZ does not affect Dates #9856
Comments
I suspect the reason this happens is because tests intentionally don't have access to the real process.env object: https://github.com/facebook/jest/blob/5ba0cc9cd3bd865cc3ad08e34e023a1e9aee7d6b/packages/jest-util/src/createProcessObject.ts#L19-L80 Practically speaking, the alternatives are to lose isolation between concurrent tests by turning (Also, if you want to set an environment variable across all tests, you can write to the real |
Yep, I realised not long after writing this issue by going through the jest setup code. It's a bit frustrating this can't be done (I do a lot of work with timezones...) but understandable. |
FWIW, we do I'd probably use some formatter other than
|
Not going to disagree!
But since part of the codebase uses Date/moment to new up instances that relies on the current environment. Think of testing a big formik form as if you were in that country. For anyone googling this issue, the workaround we've used for now is to use npm scripts to run the test in several timezones using the real unix environment variables. That works, but is a bit limiting as we can't really test what happens when data goes between timezones well. |
I don't think Temporal is gonna solve this issue. Right now the workaround is to actually polyfill so u have control over tz... it's not great. |
I just ran into this behaviour before running across this bug. Not sure if it helps but I created a couple reproducible test cases: https://github.com/bmaupin/jest-timezone-bug |
Duplicate (or: special case) of #9264 |
See #9264 (comment) for potential Jest 28-specific solution |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
Nodejs 13+ allows for setting
process.env.TZ
and having it affect new Dates created at runtime. You can try this in a repl with the following (comments are the dates output on my system):This also works in a simple node script (i.e.
node test.js
) or in the mocha testing library.Doing the same in jest, either with the lines copied into an test, or some variation where the timezone is set in
beforeEach()
, does not work. The time printed before and after setting the timezone is the same, ignoring the change in system timezone.To Reproduce
Steps to reproduce the behavior:
Expected behavior
Note that that the date serialisation should be affected by the timezone in the second
console.log()
Link to repl or repo (highly encouraged)
Unfortunately the linked repl uses nodejs 12 and so won't benefit from the change merged in nodejs 13.
envinfo
The text was updated successfully, but these errors were encountered: