Skip to content

The test NODE_ENV isn't being set properly #17032

Closed
@nicholaschiang

Description

@nicholaschiang

Bug report

Describe the bug

Currently, Next.js only supports three different values for process.env.NODE_ENV (as documented here): development, production, and test.

When running NODE_ENV='test' next build, Next.js loads the right .env.test files but fails to properly replace process.env.NODE_ENV references with test (instead of production).

Previous issues (e.g. #9123, #12772) have requested support for more environment variables. This is not what this issue is about. This is an issue with behaviour that Next.js has already documented as supported (e.g. @Timer says that test is supported in this comment).

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Clone this repository:
$ git clone https://github.com/nicholaschiang/env-demo && cd env-demo
  1. Install dependencies:
$ yarn install
  1. Build the app using the test environment:
$ yarn test:build
  1. Start the app using the test environment:
$ yarn test:start
  1. Visit the API route at http://localhost:3000/api/env and notice that the JSON response contains production instead of test. This is because Next.js hardcodes (i.e. replaces) the process.env.NODE_ENV variable to production when running next build. This is not ideal when you want to use the NODE_ENV variable to trigger certain behavior during tests (e.g. accessing different Algolia search indexes).

Expected behavior

When running next build with the NODE_ENV set to test, Next.js should properly replace process.env.NODE_ENV references with test instead of production.

System information

  • OS: PopOS 20.x
  • Version of Next.js: 9.5.3
  • Version of Node.js: 12.18.3

Additional context

This can be used to switch between outside resources (e.g. using a different set of Algolia indexes than the ones used in production) during tests. While this might not be ideal, it's required when running integration tests for me (because I can't easily stub out Algolia's search capabilities).

Metadata

Metadata

Assignees

Labels

TestingRelated to testing with Next.js.please verify canaryPlease verify the issue with the latest canary branch.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions