Skip to content
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

[heft] NODE_ENV env variable not set during test phase #2420

Closed
anthony-legay opened this issue Jan 7, 2021 · 3 comments · Fixed by #4357
Closed

[heft] NODE_ENV env variable not set during test phase #2420

anthony-legay opened this issue Jan 7, 2021 · 3 comments · Fixed by #4357
Labels
effort: easy Probably a quick fix. Want to contribute? :-) enhancement The issue is asking for a new feature or design change help wanted If you're looking to contribute, this issue is a good place to start!

Comments

@anthony-legay
Copy link

Summary

So, the issue is pretty simple: Jest, as specified in its documentation should set 2 environment variables during its execution:
JEST_WORKER_ID and NODE_ENV (to value test).

Using heft test command which then calls Jest, it appears the NODE_ENV variable is not set, only JEST_WORKER_ID.
This breaks any test specific logic which uses this env variable.

As discussed with @octogonz:

However personally, I would prefer NOT to rely on something as brittle as an environment variable. It would be better to declare an API like if (TEST) { } or if (jestEnv.TEST) { } that can have proper TypeScript typings. My team was recently asked to set up something similar for passing down Webpack variables, so maybe these can be combined into a single API.

@octogonz octogonz added enhancement The issue is asking for a new feature or design change needs design The next step is for someone to propose the details of an approach for solving the problem labels Jan 8, 2021
@octogonz
Copy link
Collaborator

octogonz commented Jan 8, 2021

Thanks for reporting this. I'll be back from vacation next week and can take a look.

Supporting the environment variable is relatively easy (and maybe could be done as a first PR to get you unblocked).

But we should work out the "right way" to pass down environment settings when using Heft. Seems like Webpack variables and Jest variables should be combined into a single global API.

@octogonz octogonz added effort: easy Probably a quick fix. Want to contribute? :-) help wanted If you're looking to contribute, this issue is a good place to start! and removed needs design The next step is for someone to propose the details of an approach for solving the problem labels Mar 9, 2021
@octogonz
Copy link
Collaborator

octogonz commented Mar 9, 2021

Let's use this issue to track the "easy" fix of exposing NODE_ENV to Heft's Jest environment. It is the title of this issue. If we want to provide a better way, we can create a separate issue to track that idea (or just make a PR).

@Lebowskovitch are you able to make a PR? Heft's Jest config is set up here.

@iclanton iclanton moved this to High priority in Bug Triage Aug 15, 2023
@woss
Copy link
Contributor

woss commented Sep 25, 2023

Since heft-jest-plugin is using the @jest/core the NODE_ENV is not set at all because jest sets it in the CLI and REPL scripts.

One way is to add the new optional field setNodeEnvVar to IJestPluginOptions where the default value would be true. If unchanged this would behave precisely as jest implementation:

NODE_ENV
Set to 'test' if it's not already set to something else.

If you agree with this I'll make a PR and add it, this would save me a lot of work of setting up the NODE_ENV for over 40 pacakges

Screenshot from the jest repo:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: easy Probably a quick fix. Want to contribute? :-) enhancement The issue is asking for a new feature or design change help wanted If you're looking to contribute, this issue is a good place to start!
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants