W
Wasp-lang•2w ago
JakeLoew

Is there a way to inject env variables into wasp testing?

I have code that throws an error if a value from .env.server is not present. This code runs on the server. I can do something like MY_SECRET=abc123 wasp client test run, but I'm wondering if there's a way to do it within the wasp api.
7 Replies
kapa.ai
kapa.ai•2w ago
Hi @JakeLoew. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I do not have enough information to answer your question specifically about injecting environment variables into Wasp testing. The knowledge sources provided don't contain information about Wasp's testing capabilities or how to inject environment variables into tests. However, I can provide some general information about environment variables in Wasp: 1. Wasp currently uses .env.server for server-side environment variables and .env.client for client-side environment variables during development Env Variables (https://wasp-lang.dev/docs/project/env-vars#1-using-env-dotenv-files). 2. For server-side code, you can access environment variables using process.env.SOME_VAR_NAME Env Variables (https://wasp-lang.dev/docs/project/env-vars#server-env-vars). 3. Wasp does not currently provide a built-in way to test server-side code. There's an open issue to add support for this in the future Testing Your Server-Side Code (https://wasp-lang.dev/docs/project/testing#testing-your-server-side-code). Given this information, it seems that Wasp doesn't currently have a specific API for injecting environment variables into tests. The method you mentioned (MY_SECRET=abc123 wasp client test run) is likely the best approach for now, as it sets the environment variable for that specific command execution. If you need more specific information about testing and environment variables in Wasp, you might want to check the latest documentation or ask in the Wasp community forums, as the feature set may have been updated since the information in these knowledge sources was compiled. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @JakeLoew, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
JakeLoew
JakeLoewOP•2w ago
Thank you. I think I can just set them in my vitest block like so:
beforeEach(() => {
process.env.MY_SECRET = 'abc123';
})
beforeEach(() => {
process.env.MY_SECRET = 'abc123';
})
Works just fine for my case.
martinsos
martinsos•2w ago
@JakeLoew I am glad you got it working! I am curious, why do you need this? Why not just set it in .env.server, what incentivized you to look for another way to set these env values?
JakeLoew
JakeLoewOP•2w ago
I'm running my tests with wasp test client -- the code doesn't seem to be reading in the values from .env.server. Do you expect that it should?
martinsos
martinsos•2w ago
@JakeLoew wasp test client runs tests for your client code, so therefore it doesn't load .env.server, since that is for the server code! Client tests run via wasp test client are ran without the server, so you can test your react compnents that way and similar. Are you trying to do something else, what is it you are trying to do?
JakeLoew
JakeLoewOP•2w ago
The test is for a class that is used on the server, but it's just typescript code and doesn't necessarily have any server dependencies other than those environment variables. It's okay though, I'm not hung up on it 🙌
martinsos
martinsos•3d ago
Got it! In that case, wasp test client is not the rigth way to test it, since it is server code. I would instead recommend setting up tests for the backend, which I believe you should be able to do manually, so not via wasp test client, but by adding the testing library in package.json yourself, and probably defining a script in package.json to run it, and doing setup for it yourself.
Want results from more Discord servers?
Add your server