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.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

9reactions
erkdecommented, Nov 25, 2020

@tremby see: https://jestjs.io/docs/en/configuration#globalsetup-string e.g.

jest.config.js

module.exports = {
  ...
  globalSetup: '<rootDir>/test/setupEnv.ts',
}

test/setupEnv.ts

import { loadEnvConfig } from '@next/env'

export default async () => {
  loadEnvConfig(process.env.PWD)
}
6reactions
howieweinercommented, Oct 15, 2020

I too have just noticed this. NODE_ENV is set to to test, but when running Jest unit tests the .env.test file is not loaded. The docs do indicate that they should.

As per @erkde comment, I have fixed this with by loading the env files in jest.config.js

const { loadEnvConfig } = require('@next/env')
loadEnvConfig(process.env.PWD)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel 5.8 .env.testing file is not working - Stack Overflow
The testing environment variables can be configured in the phpunit. xml file, but make sure to clear your configuration cache using the config:clear...
Read more >
Serverless Dotenv Plugin
Preload function environment variables into Serverless. Use this plugin if you have variables stored in a .env file that you want loaded into...
Read more >
Env Variables and Modes - Vite
Vite uses dotenv to load additional environment variables from the ... Vite is executed have the highest priority and will not be overwritten...
Read more >
Testing (Symfony Docs)
In the test environment, these env files are read (if vars are duplicated in them, files lower in the list override previous items):...
Read more >
5 reasons why your .env environment variables don't work
Some dev servers include prefixes for environment variables that should be included in the client bundle. The fix Check the docs for your...
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