我有以下代码:
var Nightmare = require('nightmare');
var tester = require('./../_common/tester');
var testCases = require('./test-cases');
var config = require('./config.js');
var data = {
nightmare: Nightmare(config.nightmare),
config: config,
testCases: testCases,
tester: tester
};
此数据主要包含配置变量和函数。
然后我有以下一组功能需要所有这些配置和功能:
var resultsPromise = pipeline([
commonFunctions.accessURL(data),
commonFunctions.loginToWebsite1(data),
commonFunctions.loginToWebsite2(data),
commonFunctions.loginToWebsite3(data),
commonFunctions.accessURL2(data),
])
.then(() => commonFunctions.success(nightmare))
.catch((error) => console.log(error));
我正确传递数据吗?有没有其他方法可以使这些函数的变量全局化?
答案 0 :(得分:3)
这很好但是如果你想在许多文件中使用这个配置,你可以使用全局关键字使任何变量或对象全局
global.data = {}
可以在任何地方访问数据对象
答案 1 :(得分:0)
global.Nightmare = require('nightmare');
在依赖项文件中使用它