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.

`next/jest` sets `NODE_ENV` to `'production'`

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ijjkcommented, 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.

1reaction
mrmckebcommented, Jan 6, 2022

Thanks @ijjk!

Read more comments on GitHub >

github_iconTop Results From Across the Web

nextjs - next build with NODE_ENV=development
If you next build && next start it will produce a production build. To achieve what you are trying to do use .env.development...
Read more >
non-standard-node-env - Next.js
The greater React ecosystem treats NODE_ENV as a convention, only permitting three (3) values: production : When your application is built with next...
Read more >
Set Environment Variables in Next.JS - Deni Apps
Before nextJS 9.4, we could use next.config.js to set up environment ... NODE_ENV !== ... and .env.production (production environment).
Read more >
how to set node_env=test in node js Code Example
set NODE_ENV =production.
Read more >
next.js multiple environments build | by Dzung Nguyen - Medium
So if you pass NODE_ENV=production to “run command”, Next will not treat your app as ... you can give me the feedbacks. Thank...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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