简体   繁体   English

引用环境变量进行Mocha测试

[英]Referencing Environment Variables for Mocha Tests

I'm trying to use dotenv to allow me to set some environment variables for my integration tests. 我正在尝试使用dotenv允许我为集成测试设置一些环境变量。 I don't want to specify the environment variables from a mocha cli script. 我不想从mocha cli脚本中指定环境变量。 I'd like to define them somewhere else like either in the test.js files themselves somewhere (preferably) or in a .env file. 我想在其他地方定义它们,例如在test.js文件本身中(最好)或.env文件中。

I'm trying to run these tests which are not based on a website environment inside create-react-app. 我正在尝试运行这些测试,这些测试不是基于create-react-app内部的网站环境。 I figured I could just add dotenv like I did below but it's not working: 我想我可以像下面一样添加dotenv,但是它不起作用:

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. 运行此命令时, env.FAKE_BRANCH未定义。 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. 我的create-react-app项目的根目录中有一个.env文件,但是由于react-create-app在后台执行了奇怪的sh **操作,因此可能找不到该文件。

I tried to move the .env file to within the folder that contains these spec.js files but no luck either. 我试图将.env文件移动到包含这些spec.js文件的文件夹中,但也没有运气。

You may need to call config on the dotenv module first: 您可能需要先在dotenv模块上调用config

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. 另一种选择是创建一个设置功能,以在测试中完全创建一个模拟环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM