我正在使用 Nightwatch 对我的 Vue js 应用程序进行一些测试。由于我已将测试集成到 gitlab ci/cd 中,因此我需要两种不同的 Nightwatch 配置。
为了实现这一目标,我做了以下工作:
为本地和 gitlab 测试创建两个不同的脚本
"scripts" : {
"e2e" : "node test/e2e/gitlab_runner.js" ,
"local-test" : "node test/e2e/local_runner.js" ,
"test": "npm run e2e"
然后我将所需的变量嵌入到 local_runner
和 gilab_runner
文件中
process.env.NIGHTWATCH_CONFIG = 'nightwatch.local.js';
require('./runner.js');
以及 gitlab_runner
process.env.NIGHTWATCH_CONFIG = 'nightwatch.conf.js';
require('./runner.js');
然后在文件 runner.js
中我有以下代码行
opts = opts.concat(['--config', 'test/e2e/' + NIGHTWATCH_CONFIG]);
这样我就可以获得正确的文件。
问题是当我运行脚本时出现以下错误:
(node:21061) UnhandledPromiseRejectionWarning: ReferenceError: NIGHTWATCH_CONFIG is not defined
at devConfigPromise.then.then (xxxxxx/test/e2e/runner.js:30:51)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:697:11)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
(node:21061) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21061) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我还尝试在 local_runner
和 gitlab_runner
文件中执行以下操作:
module.exports = {
NIGHTWATCH_CONFIG = 'nightwatch.local.js';
};
但是 runner.js
文件中的 NIGHTWATCH_CONFIG
变量未定义。
我该如何解决这个问题?
最佳答案
undefined reference 错误NIGHTWATCH_CONFIG
,因为在调用环境变量process.env
时需要指定。
opts = opts.concat(['--config', 'test/e2e/' + process.env.NIGHTWATCH_CONFIG]);
关于javascript - 在 javascript 文件中添加 NodeJS 的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600300/