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

Environment Variables in Test #20717

Closed
mcgear opened this issue Sep 27, 2023 · 5 comments
Closed

Environment Variables in Test #20717

mcgear opened this issue Sep 27, 2023 · 5 comments

Comments

@mcgear
Copy link

mcgear commented Sep 27, 2023

Hey, I am curious as to if deno test supports the environment variables? It seems that maybe it doesn't as my tests do not work when they rely on environment variables. Variables are just set in a .env file, should this work with tests?

@bartlomieju
Copy link
Member

Yes, you can access env vars in test using Deno.env APIs. How are you setting up these env vars from .env when running the tests?

@mcgear
Copy link
Author

mcgear commented Sep 27, 2023

I just have the .env file in a format like

ENV_VARIABLE=the-value
...

What i did to get it working was create a .env.ts file and use the Deno.env.set("ENV_VARIABLE", "the-value"). And then in the test that needs the env variables, i just do an import '../../.env.ts'.

I was unable to get it working by simply having the .env file at the root (as does work in a deno run) context.

@mcgear
Copy link
Author

mcgear commented Sep 27, 2023

I should also note this is the command i had tried at one point to try and register the .env file:

    "test": "deno test -A --unstable --allow-env .env tests/tests.ts --coverage=cov"

@dsherret
Copy link
Member

I think you're wanting the --env feature. That will be coming hopefully in the next release: #20300

For now, there's the deno_std module: https://deno.land/std@0.203.0/dotenv/mod.ts

// add this to your code
import "https://deno.land/std@0.203.0/dotenv/load.ts";

@mcgear
Copy link
Author

mcgear commented Sep 28, 2023

Perfect, thank you, this was exactly what i needed. Thanks again for the help.

@mcgear mcgear closed this as completed Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants