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

Clarification on how .env files can be loaded with Jest #16443

Closed
wants to merge 1 commit into from

Conversation

jportela
Copy link

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

Copy link
Contributor

@prkagrawal prkagrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

Comment on lines +128 to +136
### 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
```
Copy link
Member

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.

@tmikeschu
Copy link

tmikeschu commented Oct 18, 2020

👋 thanks for looking into this! Just wanted to add that I added this to my setup file and got the error:

Could not find a valid build in the './.next' directory! Try building your app with 'next build' before starting the server.

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 dotenv to load the file.

import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });

neilvanbeinum added a commit to LBHackney-IT/repairs-hub-frontend that referenced this pull request Jan 19, 2021
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
neilvanbeinum added a commit to LBHackney-IT/repairs-hub-frontend that referenced this pull request Jan 19, 2021
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
neilvanbeinum added a commit to LBHackney-IT/repairs-hub-frontend that referenced this pull request Jan 21, 2021
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
neilvanbeinum added a commit to LBHackney-IT/repairs-hub-frontend that referenced this pull request Jan 21, 2021
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
@timneutkens
Copy link
Member

Going to close this as the PR is stale.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants