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

next/jest sets NODE_ENV to 'production' #33005

Closed
mrmckeb opened this issue Jan 4, 2022 · 5 comments · Fixed by #33032
Closed

next/jest sets NODE_ENV to 'production' #33005

mrmckeb opened this issue Jan 4, 2022 · 5 comments · Fixed by #33032
Assignees

Comments

@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 4, 2022

Run next info (available from version 12.0.8 and up)

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101
Binaries:
  Node: 14.17.6
  npm: 8.1.4
  Yarn: 1.22.15
  pnpm: N/A
Relevant packages:
  next: 12.0.8-canary.17
  react: 17.0.2
  react-dom: 17.0.2

What version of Next.js are you using?

12.0.8-canary.17

What version of Node.js are you using?

14.17.6

What browser are you using?

N/A

What operating system are you using?

macOS 12.0.1 (21A559)

How are you deploying your application?

N/A

Describe the Bug

The next/jest preset sets NODE_ENV to 'production'.

Users familiar with Jest will expect NODE_ENV to be 'test' when running tests. The documentation says:

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

In this case, NODE_ENV is indeed set to 'test' initially, but is replaced with 'production'. I've verified this simply by console logging the environment in the test setup files, and again in a test. I did take a quick look, but wasn't able to identify the cause.

This is related to #17032 and #17903.

Expected Behavior

When running tests in Jest, NODE_ENV should be 'test'.

To Reproduce

Create a new app using the Jest example:

npx create-next-app --example with-jest with-jest-app

Add the following line to the example test:

// __tests__/index.test.tsx
console.log(process.env.NODE_ENV); // production

Run:

npm test
@mrmckeb mrmckeb added the bug Issue was opened via the bug report template. label Jan 4, 2022
@mrmckeb
Copy link
Contributor Author

mrmckeb commented Jan 5, 2022

As a workaround, users could create a util like this:

export const isTestEnv = () => !!process.env.JEST_WORKER_ID;

@franciscobonand
Copy link

Having this same problem here. Another thing that I've noticed is that console.log(process.env.NODE_ENV) prints "production", but when I print console.log(process.env) and search for NODE_ENV the value is NODE_ENV: 'test'

@styfle styfle added kind: bug and removed bug Issue was opened via the bug report template. labels Jan 5, 2022
@ijjk
Copy link
Member

ijjk commented Jan 5, 2022

Hi, this has been updated in v12.0.8-canary.18 of Next.js, please update and give it a try!

Note: you may need to run yarn jest --clearCache for this change to take affect.

@mrmckeb
Copy link
Contributor Author

mrmckeb commented Jan 6, 2022

Thanks @ijjk!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants