Skip to content
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

Env variables not loaded for unit tests purpose #22936

Closed
lipoolock opened this issue Mar 10, 2021 · 3 comments
Closed

Env variables not loaded for unit tests purpose #22936

lipoolock opened this issue Mar 10, 2021 · 3 comments
Labels
bug Issue was opened via the bug report template.

Comments

@lipoolock
Copy link

lipoolock commented Mar 10, 2021

What version of Next.js are you using?

v10.0.9-canary.4

What version of Node.js are you using?

v14.16.0

What browser are you using?

Firefox

What operating system are you using?

Linux

How are you deploying your application?

no

Describe the Bug

If you set some environment variables in .env and .env.test they are not taken into account while playing unit tests and tests based on these variables fail

Expected Behavior

With NextJs 10.0.6 it works well

To Reproduce

You can login to sandbox and play the unit tests set here https://codesandbox.io/s/laughing-leaf-zfp7k?file=/__tests__/testing-library.js

If you run yarn test __tests__/testing-library.js you will get undefined instead of fooBar for the env variable set in .env.test

@lipoolock lipoolock added the bug Issue was opened via the bug report template. label Mar 10, 2021
@ijjk
Copy link
Member

ijjk commented Mar 10, 2021

Hi, Next.js isn't being invoked at all when running tests in the above set-up, you need to load the env manually with loadEnvConfig from @next/env. The above reproduction also fails on v10.0.6 since we never were loading environment variables at a per-page basis.

Closing as this doesn't appear to be a bug in Next.js and instead with the test set-up

@lipoolock
Copy link
Author

Hello @ijjk
Thanks a lot for explanations
I just forgot that I previously used a workaround in jest.config proposed by @timneutkens there #1790
So I added it to the sandbox shared in this ticket.

But that requires to make the build of NextJs before launching tests
And this workaround doesn't work with the latest release by the way

But, by having a look again on that topic #1790 and thanks to your help, I have been able to make working my unit test

Below step to fix this issue

  • Install @next/env
  • In jest.config add this line globalSetup: '<rootDir>/test.env.js',
  • in test.env.js add the block below
import { loadEnvConfig } from '@next/env'
import { resolve } from 'path'

export default async () => {
  const envFile = resolve(__dirname, '.')
  loadEnvConfig(envFile)
}

I would suggest to add this example in your doc, that could help all of us (as required by this (still opened) issue
#17903

kodiakhq bot pushed a commit that referenced this issue Mar 12, 2021
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
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants