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

Docs: Set NODE_ENV to test for Accurate Environment Variable Loading with Cypress #54263

Closed
phil-loops opened this issue Aug 19, 2023 · 2 comments
Labels
Documentation Related to Next.js' official documentation. locked

Comments

@phil-loops
Copy link

phil-loops commented Aug 19, 2023

What is the improvement or update you wish to see?

I would like to address an inaccuracy in the docs with regards to testing Next.js (as a user of a Next.js app).

The Next.js docs state:

This one is useful when running tests with tools like jest or cypress where you need to set specific environment vars only for testing purposes. Test default values will be loaded if NODE_ENV is set to test, though you usually don't need to do this manually as testing tools will address it for you.

At least with regards to Cypress, the user must opt-in to the testing environment. I am open to playing around with Jest, though I suspect based on their docs that again the user will need to opt next.js into the testing environment.

In other words, to test a Next.js app with Cypress, I believe it is a better pattern to run NODE_ENV=test next dev to leverage Next.js's NODE_ENV-sensitive environmental variable load order

Is there any context that might help us understand?

I threw up a quick proof of concept demonstrating that the package.json script must specify the NODE_ENV when starting the Next.js process in order to access .env.test. (Note: this project was initialized with npx create-next-app --example with-cypress with-cypress-app)

The key takeaway is that reverting phil-loops@19edb21 causes the cypress tests to fail, since the changed test is expecting the value from .env.test. Only by explicitly telling Next.js that we want to start in the test environment can the test pass.

Does the docs page already exist? Please link to it.

https://github.com/vercel/next.js/tree/canary/examples/with-cypress

@phil-loops phil-loops added the Documentation Related to Next.js' official documentation. label Aug 19, 2023
@leerob
Copy link
Member

leerob commented Nov 9, 2023

Hmm, I'm not sure. As far as I know, I haven't seen this reported anywhere else and people have been successful with the Cypress example. Could you maybe consider opening an issue on their side and asking them to clarify the best practices? I believe we chatted with them when this example / docs were created.

@leerob leerob closed this as completed Nov 9, 2023
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Related to Next.js' official documentation. locked
Projects
None yet
Development

No branches or pull requests

2 participants