尝试使用NPM脚本指定node_env变量,但我得到未定义的属性错误

时间:2015-12-14 19:13:52

标签: javascript node.js npm

我创建了一个variables.json,用于托管不同环境的静态设置。

我想在test.js文件中做的是指定我将使用我在variables.json文件中定义的变量使用哪个站点。

我编写了一系列NPM脚本来帮助,我认为会从variables.json中指定本地或生产对象。

这是我的变量.json:

{
  "local": {
    "domainLogin": "https://admin.local.us/en-US/signin",
    "domainDash": "https://admin.local.us/en-US/trunk-slug/branch-slug/dashboard"
  },
  "production": {
    "domainLogin": "http://admin.production.io/en-US/signin",
    "domainDash": "http://admin.production.io/en-US/trunk-slug/branch-slug/dashboard"
  }
}

这是我的test.js脚本:

var driver = require('selenium-webdriver');
var chai = require('chai');
var expect = chai.expect;

var config = require('../../../../../variables');

chai.use(require('chai-as-promised'));

describe('Admin Tests - User Alice', function() {
  var domainVar = process.env.envVar;
  var domain = config.domainVar.domainLogin;
  var dashboard = config.domainVar.domainDash;

  before(function() {
    this.timeout(50000);
    this.driver = new driver.Builder().withCapabilities(driver.Capabilities.firefox()).build();
    this.driver.get(domain);

    return this.driver.sleep(250);
  });

  after(function() {
    return this.driver.quit();
  });

  describe('would like to', function() {
    it('verify they are on login screen', function() {
      this.driver.sleep(250);

      var formButton = this.driver.findElement({
        css: 'button[type="submit"]'
      });

      return expect(this.driver.getCurrentUrl()).to.eventually.equal(domain);
    });
  });

  describe('login with their credentials', function() {
    beforeEach(function() {
      this.driver.sleep(500);

      this.driver.findElement({
        css: 'input[name="email"]'
      }).sendKeys('user@example.com');

      this.driver.findElement({
        css: 'input[name="password"]'
      }).sendKeys('example');

      var formButton = this.driver.findElement({
        css: 'button[type="submit"]'
      });

      this.driver.actions()
        .mouseMove(formButton)
        .click()
        .perform();

      this.driver.sleep(250);
    });

    it('verify they are in admin dashboard', function() {
      this.driver.sleep(500);
      return expect(this.driver.getCurrentUrl()).to.eventually.equal(dashboard);
    });
  });
});

以下是我使用的NPM脚本:

{
  "scripts": {
    "env-local": "env envVar='local'",
    "env-prod": "env envVar='production'",
    "el-2378-p:spec": "npm run env-local mocha test/spec/admin/dashboard/alice/test.js -- --reporter spec --slow 0",
    "test": "mocha"
  }
}

以下是运行测试时出现的错误:

var domain = config.domainVar.domainLogin;
                               ^

TypeError: Cannot read property 'domainLogin' of undefined
    at Suite.<anonymous> (/test.js:11:32)
    at context.describe.context.context (/node_modules/mocha/lib/interfaces/bdd.js:47:10)
    at Object.<anonymous> (/test.js:9:1)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at /node_modules/mocha/lib/mocha.js:216:27
    at Array.forEach (native)
    at Mocha.loadFiles (/node_modules/mocha/lib/mocha.js:213:14)
    at Mocha.run (/node_modules/mocha/lib/mocha.js:453:10)
    at Object.<anonymous> (/node_modules/mocha/bin/_mocha:393:18)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
    at node.js:974:3

如果我改变test.js脚本中的行:

,那就太奇怪了
  var domainVar = process.env.envVar;
  var domain = config.domainVar.domainLogin;
  var dashboard = config.domainVar.domainDash;

为:

  var domainVar = process.env.envVar;
  var domain = config.local.domainLogin;
  var dashboard = config.local.domainDash;

它会工作,但它使我的domainVar无用。我要做的是指定我想在NPM脚本中使用哪些变量。如果我必须在本地和生产之间进行指定,我将需要同一脚本的多个版本,并且test.js文件不会像我希望的那样松散耦合。

当我在test.js文件中的console.log(domainVar)时,我得到了输出:

local

这让我怀疑虽然我可以输出一个带有node_env变量的字符串,但我的test.js文件不能将它识别为附加的有效属性?

如果这是真的,为什么会发生这种情况?当我创建变量domainVar时,它不会简单地替换它所代表的文本(本地)吗?为什么

config.local.domainLogin不等同于config.domainVar.domainLogin

有没有比我上面所做的更好的验证环境变量的方法?

1 个答案:

答案 0 :(得分:2)

很难理解你的意思,或者你想要做什么,但我会采取刺痛......

我怀疑你认为这样做

var domainVar = process.env.envVar;
var domain = config.domainVar.domainLogin;
var dashboard = config.domainVar.domainDash;

假设domainVar等于"local",您将获得相当于

的内容
var domain = config.local.domainLogin;
var dashboard = config.local.domainDash;

但这根本不是这样的。

具有特定标识符的变量存在的事实不会“覆盖”在其他地方使用该名称(标识符...)。如果要使用变量的值来访问对象的属性,请使用方括号:

var domainVar = process.env.envVar;
var domain = config[domainVar].domainLogin;
var dashboard = config[domainVar].domainDash;