Environment Variables
How to include environment variables in your Asserted test at runtime
It's likely that at some point you'll need to include some sensitive information like test tokens or usernames in your Asserted tests. Follow the instructions below to do so securely without saving these values directly in your git repo.
Included in the fixed dependencies is dotenv and getenv. These are simple libraries that are commonly used to pull in secrets or configuration from environment variables.
dotenv
loads environment variables from an.env
file into process.envgetenv
reads the variables out of process.env and throws if they are missing rather than defaulting to an empty variable
If you plan on publishing to NPM it is extremely important that you exclude your .asserted
directory if you have included a .env file in it.
Use the "files" property of your root package.json to include only the files and folders you want when publishing to NPM.
Storing Environment Variables
To access environment variables at runtime in Asserted, while still not checking them into your repo, do the following:
Create a
.env
file containing the variables within the.asserted/
directory of your projectEnsure that the
.gitignore
file inside the.asserted/
directory includes a reference to the.env
file you just added (it should by default)Add an entry to the
package.json
in your.asserted/
directory for"files": [ "**/*.js", ".env" ]
, this will include the.env
file in your package when you runasrtd push
If your root package is going to be published to NPM, exclude
.asserted
from being published
After doing the above, when you run asrtd push
, you should see the .env
file listed in the files includes in the routine package, while still omitting the file from your git repo.
Reading Environment Variables
Populate process.env
from the variables stored in the .env
file using the following:
It's best to use getenv
to read whatever environment variables you're interested in, but you can just read directly from process.env
if you prefer.
Last updated