How to manage NODE_ENV while testing API routes #16265
Unanswered
ChristopherPAndrews
asked this question in
Help
Replies: 0 comments
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.
-
I have been struggling to test my API routes. I finally found this discussion, which got me much closer and seems to make sense.
I made a local copy of some of the tools from the next-testing-utils as suggested. The process of invoking
next build
and then starting up the server seems to work. However, my code uses NODE_ENV to determine which database setup to use ('production', 'development', or 'test'). I am using jest, so the environment should be set to 'test', but when I run my tests it would try to access the production DB. When I logged process.env.NODE_ENV, it reported 'production'. After thinking I was going insane (logging process.env showed NODE_ENV='test'), I discovered that deep under the hood webpack is just swapping out the string process.env.NODE_ENV with 'production' in my code (which I confirmed by looking at the generated code).I can work around this with an additional environment variable in my code, but I am wondering if anyone has any thoughts about how to do this more elegantly (in essence, how can I make "test" builds that don't claim to be production).
Beta Was this translation helpful? Give feedback.
All reactions