-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Misleading documentation: variables in .env and .env.test are not being loaded in test environment #17903
Comments
As of v9.5.5, you can just do
if you add that your jest setup (e.g. globalSetup), should be good to go |
So the docs are out of date? |
maybe, 9.5.5 is only out a few days |
Well the problem also exists on 9.5.4. It's either an application bug or a documentation bug regardless, unless I have something wrong. |
Next.js does not set up your test environment currently so it can't load environment variables for you. You can boot up the Next.js server though which loads the environment variables:
|
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
|
A documentation bug then. Regarding booting Next, do you suggest doing that globally (such as in jest config) or per test file which needs it? |
For my purposes, it made sense to make the change globally as I had an env var that disables auth checks during tests. The loading if .env.test does work for my Cypress tests as documented |
But only after booting Next, right? That ought to be documented. |
Sorry, what do you mean by 'booting Next'? |
As above in @timneutkens's comment:
|
Ah. Sorry, I didn't seem to need this step. Programmatically setting the env vars using loadEnvConfig was enough |
Right, so it sounds like either |
My temporary workaround has been to add a global setup file in my Jest configuration: /**
* @file jest.config.js
*/
module.exports = {
globalSetup: '<rootDir>/test/setup.ts',
}; and to load a particular environment configuration with /**
* @file test/setup.ts
*/
import dotenv from 'dotenv';
dotenv.config({ path: '.env.test' }); I know it doesn't solve the problem, but hopefully it can be helpful in the meantime. Also, you may be able to run dotenv.config({ path: '.env' });
dotenv.config({ path: '.env.test' }); |
My workaround was actually simpler (but arguably worse) since I only need
two very specific things set: I forego the .env files for test purposes and
just directly set process.env.whatever = "whatever" in the setup file. Does
the trick.
|
I needed the .env.test file loaded for both Jest and out Cypress integration tests. My workaround was to use https://www.npmjs.com/package/dotenv-load which behaves as expected. |
I just came back to this. I don't want to install dotenv, since that doesn't necessarily work in exactly the same way as next would load the vars. Putting I tried the suggestion in this comment. import { loadEnvConfig } from "@next/env";
import { resolve } from "path";
loadEnvConfig(resolve(__dirname, "..")); but then the test console output is littered with log lines from the env files being loaded -- loadEnvConfig(resolve(__dirname, ".."), undefined, { info: jest.fn(), error: console.error }); but My workaround for now is heavy-handed: const mockInfo = jest.spyOn(console, "info").mockImplementation();
loadEnvConfig(resolve(__dirname, ".."));
mockInfo.mockRestore(); |
@tremby see: https://jestjs.io/docs/en/configuration#globalsetup-string e.g. jest.config.js
test/setupEnv.ts
|
Ok, now it only runs once, which is better. Thanks. Not that it matters any more, but that function still doesn't seem to pay any attention to its third parameter. The point of this ticket remains that the docs are misleading, which is still an issue. |
This adds a note to the environment variables documentation to mention how the env files can be loaded using the `@next/env` package as this has been brought up a few times. x-ref: #22936 (comment) x-ref: #17903
Is there any plan to fix this? |
Please verify that your issue can be recreated with Why was this issue marked with the
|
This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
According to these docs:
Further down on the same page:
But variables in
.env
and.env.test
are not being loaded when running tests.To Reproduce
Basic setup and
.env
:create-next-app -e with-jest
and enter a namecd <name>
echo MY_VAR=my_value >.env
npm run test:ci -- env
The test fails:
process.env.MY_VAR
is undefined..env.test
:mv .env .env.test
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
The text was updated successfully, but these errors were encountered: