Is it possible to use React Testing Library with NEXT_PUBLIC_ environment variables? #16315
Unanswered
sophiekoonin
asked this question in
Help
Replies: 1 comment 3 replies
-
In the jest setup you can do something like this:
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey all,
We've got a few Next apps within a monorepo, and we run
jest
from the root of that repo to run tests for all of those apps. Our tests usereact-testing-library
(RTL). We're using Next 9.5.2.I'm trying to replace the older
publicRuntimeConfig
with the more up-to-date environment variables as suggested in the docs, which is working fine when running the app, but it's broken all our tests. For example, our API path is set withprocess.env.NEXT_PUBLIC_API_PATH
, but when we render a component directly withrender
from RTL, the API path doesn't get set.Since we aren't running the Next build process when testing with RTL + Jest, the environment variables don't get substituted for the real deal (so
.env.test
does not work as suggested in the docs). I've tried to set the env vars explicitly in the test files, but this still isn't working - everything is coming through as undefined.Is there another way to do component testing while still using Next's environment variables? Should I just go back to using
publicRuntimeConfig
if I want to usereact-testing-library
?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions