Dark Matter
Dark Matter

Reputation: 29

Referencing Environment Variables for Mocha Tests

I'm trying to use dotenv to allow me to set some environment variables for my integration tests. I don't want to specify the environment variables from a mocha cli script. I'd like to define them somewhere else like either in the test.js files themselves somewhere (preferably) or in a .env file.

I'm trying to run these tests which are not based on a website environment inside create-react-app. I figured I could just add dotenv like I did below but it's not working:

require('dotenv')

    describe('some description', () => {
        let { env } = process

        it.only('creates an environment', async () => {
          const options = {
            branch: env.FAKE_BRANCH
          }
          const result = await deploy(options)
          expect(result.success).to.be.true
        })
      })

env.FAKE_BRANCH is undefined when I run this. I have an .env file in the root of my create-react-app project but it's probably not able to find it due to the weird sh** that react-create-app does behind the scenes.

I tried to move the .env file to within the folder that contains these spec.js files but no luck either.

Upvotes: 2

Views: 4518

Answers (1)

pseudoramble
pseudoramble

Reputation: 2561

You may need to call config on the dotenv module first:

require('dotenv').config();

It looks like that extra call is missing from the code above.

An alternative could create a setup function that creates a mock environment within the test entirely.

Upvotes: 1

Related Questions