Skip to content

Misleading documentation: variables in .env and .env.test are not being loaded in test environment #17903

@tremby

Description

@tremby

Bug report

Describe the bug

According to these docs:

Next.js allows you to set defaults in .env (all environments)

Further down on the same page:

Apart from development and production environments, there is a 3rd option available: test. In the same way you can set defaults for development or production environments, you can do the same with .env.test file for testing environment

But variables in .env and .env.test are not being loaded when running tests.

To Reproduce

Basic setup and .env:

  1. create-next-app -e with-jest and enter a name
  2. cd <name>
  3. echo MY_VAR=my_value >.env
  4. cat >__tests__/env.js <<EOF
    describe("environment variables", () => {
      it("loads variables from .env in the test environment", () => {
        expect(process.env.MY_VAR).toBe("my_value");
      });
    });
    EOF
    
  5. npm run test:ci -- env

The test fails: process.env.MY_VAR is undefined.

.env.test:

  1. mv .env .env.test
  2. npm run test:ci -- env

The test fails: process.env.MY_VAR is undefined.

Expected behavior

The test should pass in both situations (with the file named either .env or .env.test). At least, if I'm understanding the docs correctly and I'm not doing anything silly.

System information

  • OS: Ubuntu
  • Version of Next.js: 9.5.5
  • Version of Node.js: 12.18.1

Metadata

Metadata

Assignees

Labels

please verify canaryPlease verify the issue with the latest canary branch.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions