Vscode-jest: I need to run jest with a specific TZ

Created on 8 Oct 2017  Â·  13Comments  Â·  Source: jest-community/vscode-jest

this extension has almost all the features I need. Only thing lacking is ability to run jest with a custom ENV vars.
I run my jest with TZ=UTC so that my snapshot tests match the ones on our build server. Is there any way to resolve this? Is it something I could potentially open as a PR? It would be trivial to add a single config just for passing ENV variables.

question

Most helpful comment

Prepended jest scripts with TZ=UTC in package.json. Works great!

    . . .
    "test": "TZ=UTC jest --coverage",
    "test:watch": "TZ=UTC jest --watchAll",
    . . .

All 13 comments

Could you use jest's setup script to set the process env?

On Oct 8, 2017 06:26, "Jiri Spac" notifications@github.com wrote:

this extension has almost all the features I need. Only thing lacking is
ability to run jest with a custom ENV vars.
I run my jest with TZ=UTC so that my snapshot tests match the ones on our
build server. Is there any way to resolve this? Is it something I could
potentially open as a PR? It would be trivial to add a single config just
for passing ENV variables.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/orta/vscode-jest/issues/153, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAC_jj7G4IVSZ2Np5Y_vbaC0x5Hf4usvks5sqKNVgaJpZM4PxqMC
.

can you set a process env in jest config?
As a workaround i added
export TZ=UTC in my ~/.bash_profile

Hopefully we can do this per workspace.

Cheers for the extension really helpful

I mean using this setting in jest for that specific test suite to run something like

process.TZ = "UTC"

Before every tests suite.

@orta not sure if it's going to work- I think at that time all the child processes are already forked so they aren't started with the time in UTC. I tried it like a year ago. Will try again.

or jest's config globals...

A set of global variables that need to be available in all test environments.

you can use different jest.config file for different test setting, either use vscode-jest setting jest.pathToConfig or simply pass the config file in cli via jest.pathToJest, for example, node_modules/.bin/jest --config=utc-jest.js

it's best to utilize jest mechanism, not only it keeps vscode-jest light and clean but also enables you to run your test independent of vscode, especially when you consider running them later in any ci environments or automatic batch testing...

I was curious about this today and added

  globals: {
    TZ: 'utc',
  },

to my jest.config.js.

It didn't work. What's the solution these days? I'm in 24.5.0.

Try this before describe in your test
const moment = require.requireActual('moment-timezone').tz.setDefault('America/Los_Angeles');

Would that work if I don't use moment?

Prepended jest scripts with TZ=UTC in package.json. Works great!

    . . .
    "test": "TZ=UTC jest --coverage",
    "test:watch": "TZ=UTC jest --watchAll",
    . . .

Pure Jest only solution (no need to modify scripts in package.json)

globalSetup.js

export default () => {
  process.env.TZ = 'UTC';
}

package.json

{

  "jest": {
      "globalSetup": "<rootDir>/globalSetup.js",

  }

}

EDIT: as @jcollum mentioned, it's the same solution that @joetidee linked to: https://stackoverflow.com/a/56482581/270302

Same answer as here:
https://stackoverflow.com/questions/56261381/how-do-i-set-a-timezone-in-my-jest-config

On Fri, Oct 18, 2019 at 9:12 AM Avindra Goolcharan notifications@github.com
wrote:

Pure Jest only solution (no need to modify scripts in package.json)

globalSetup.js

export default () => {
process.env.TZ = 'UTC';
}

package.json

{

"jest": {
"globalSetup": "/globalSetup.ts",

}

}

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/jest-community/vscode-jest/issues/153?email_source=notifications&email_token=AAFMAAQDZXQKMTRGTAYG2P3QPHN47A5CNFSM4D6GUMBKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBU7JAY#issuecomment-543814787,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAFMAARGBSOWARHY7HIGF5LQPHN47ANCNFSM4D6GUMBA
.

@avindra I am the author of the SO, you should add to your solution that it does not work on Windows

Was this page helpful?
0 / 5 - 0 ratings