我创建了一个变量.json,用于托管不同环境的静态设置。
我想在 test.js 文件中执行的操作是指定我将使用在变量.json 文件中定义的变量的站点。
我编写了一系列 NPM 脚本来帮助(我认为会)从变量.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
是否有比我在上面所做的更好的方法来验证环境变量?
最佳答案
很难理解你的意思,或者你想要做什么,但我会尝试一下......
我怀疑你是这么想的
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;
关于javascript - 尝试使用 NPM 脚本指定 node_env 变量,但出现未定义的属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274826/