-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Clarification on how .env files can be loaded with Jest #16443
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome
### Loading Test Environment Variables | ||
|
||
In test environments such as `jest`, Next.js doesn't get automatically instantiated. You'll need to instantiate Next.js explicitly as part of your tests setup, in order to load `.env` files (including `.env.test`): | ||
|
||
```js | ||
// include this in your Jest setup file, or before your tests | ||
import next from 'next' | ||
next({}) // instantiates Next.js to load .env files | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels vague, as if it were missing context. Something better would be to add a list of examples to the section above this one (Test Environment Variables
), and include one with Jest that has this same code but in a context where it works.
👋 thanks for looking into this! Just wanted to add that I added this to my setup file and got the error:
This seemed odd to me because the test environment shouldn't worry about build artifacts. I eventually found that: -next({})
+next({ dev: true }) did the trick. UPDATE (10/19/20) Didn't love the next server output in the test logs, so ended up using
|
Next isn't instantiated by Jest, so we need to explicitly tell it where to get env vars. Using .env.sample as I don't see a need for more specialised .env.test or other file for our values which are all test strings. vercel/next.js#16443
Next isn't instantiated by Jest, so we need to explicitly tell it where to get env vars. Using .env.sample as I don't see a need for more specialised .env.test or other file for our values which are all test strings. vercel/next.js#16443
Next isn't instantiated by Jest, so we need to explicitly tell it where to get env vars. Using .env.sample as I don't see a need for more specialised .env.test or other file for our values which are all test strings. vercel/next.js#16443
Next isn't instantiated by Jest, so we need to explicitly tell it where to get env vars. Using .env.sample as I don't see a need for more specialised .env.test or other file for our values which are all test strings. vercel/next.js#16443
Going to close this as the PR is stale. |
This clarifies how
.env.test
files can be loaded in an environment like Jest, where Next.js doesn't get instantiated automatically. It will hopefully help with these questions #16315 #16270