question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting process.env.TZ does not affect Dates

See original GitHub issue

🐛 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):

console.log(process.env.TZ); // undefined
console.log(new Date().toString()); // Tue Apr 21 2020 22:05:28 GMT+0100 (British Summer Time)
process.env.TZ = 'Asia/Kolkata'; // not my timezone
console.log(new Date().toString()); // Wed Apr 22 2020 02:35:28 GMT+0530 (India Standard Time)

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:

describe('changing the timezone', () => {
  it('resets the nodejs timezone cache', () => {
    console.log(process.env.TZ); // undefined
    console.log(new Date().toString()); // Tue Apr 21 2020 22:36:08 GMT+0100 (British Summer Time)
    process.env.TZ = 'Asia/Kolkata'; // not my timezone
    console.log(new Date().toString()); // Tue Apr 21 2020 22:36:08 GMT+0100 (British Summer Time)
  });
});

Expected behavior

Note that that the date serialisation should be affected by the timezone in the second console.log()

describe('changing the timezone', () => {
  it('resets the nodejs timezone cache', () => {
    console.log(process.env.TZ); // undefined
    console.log(new Date().toString()); // Tue Apr 21 2020 22:37:36 GMT+0100 (British Summer Time)
    process.env.TZ = 'Asia/Kolkata'; // not my timezone
    console.log(new Date().toString()); // Wed Apr 22 2020 03:07:36 GMT+0530 (India Standard Time)
  });
});

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

$ npx envinfo --preset jest
npx: installed 1 in 1.318s

  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.0.0 - ~/.nvm/versions/node/v14.0.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v14.0.0/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:24
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
idecommented, Feb 10, 2021

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 console.log(util.types.isProxy(process.env)) prints true inside Jest and false in a regular Node REPL.

Practically speaking, the alternatives are to lose isolation between concurrent tests by turning process.env into shared, mutable state or to write a custom Jest environment that spawns processes instead of V8 contexts.

(Also, if you want to set an environment variable across all tests, you can write to the real process.env in the global setup script.)

1reaction
StringEpsiloncommented, Feb 27, 2022

Duplicate (or: special case) of #9264

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I set the default timezone in node.js? - Stack Overflow
Under Configuration -> Software -> Environment Properties , simply set the key value pair TZ and your time zone e.g. America/Los Angeles ,...
Read more >
problems with changing process.env.TZ at runtime
first convert a string to a date or vice versa. tzset() parses and caches the TZ environment variable. That means that you can...
Read more >
Time zones in Node.js - Medium
If you need to change the time zone on the fly, this is the way: process.env.TZ = "America/Montreal". I've seen some posts claiming...
Read more >
Using AWS Lambda environment variables
An environment variable is a pair of strings that is stored in a function's version-specific configuration. The Lambda runtime makes environment variables ...
Read more >
Axel Rauschmayer on Twitter: "TIL: You can set the default ...
TIL: You can set the default (local) timezone in Node.js. > process.env. TZ = 'America/Anchorage'; > new Date('2019-06-02T20:00').
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found