如何使用karma/jasmine测试未导出的ES6前JS文件



我有一个普通的JS文件,它是用ES6之前的JS代码编写的,我想使用karma/jasmine测试套件进行测试。文件很大,但我只想得到一个超级简单的例子。

// pluginV3.js
// This is the file I am trying to test. It is written in pre ES6 JS and has no exports
var myvar = null;

我发现了一个名为rewire的库,我认为它可以让我测试这样的纯JS文件,但我很难让它与我的karma/jasmine堆栈一起工作。到目前为止,我已经尝试过这个:

// var rewire = require("rewire"); // Can't use require. Test code is also pre ES6
beforeEach(module('rewire')); // I have been trying to instance "rewire" here using the exported module in rewire's index.js  
describe('V3 Helper.js', function() {
describe('$pluginV3.myvar', function () {
fit('Tests a certain variable in pluginV3 is set to an expected initial value', function () {
var pluginV3 = rewire("../../../../public/static/js2/pluginV3.js");
expect(pluginV3.myvar).toEqual(null); // expect this scope var to be something
});
});
});

但我得到了这个错误:

LOG: '----------------------------------------------------'
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
LOG: 'What is rewire?'
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
LOG: function rewire(filename) { ... }
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
Chrome 96.0.4664.55 (Mac OS 10.15.7) CN Plugin Client V3 Helper $scope.transferData Tests a certain variable in Plugin Client V3 is set to an expected initial value FAILED
TypeError: rewireModule is not a function
at rewire (node_modules/rewire/lib/index.js:11:12)
at UserContext.<anonymous> (tests/helpers/cn-plugin-client-v3-test.js:25:28)
at <Jasmine>
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 1 of 30 (1 FAILED) (0 secs / 0.002 secs)
Chrome 96.0.4664.55 (Mac OS 10.15.7) CN Plugin Client V3 Helper $scope.transferData Tests a certain variable in Plugin Client V3 is set to an expected initial value FAILED
TypeError: rewireModule is not a function
at rewire (node_modules/rewire/lib/index.js:11:12)
at UserContext.<anonymous> (tests/helpers/cn-plugin-client-v3-test.js:25:28)
at <Jasmine>

这让我认为beforeEach(module('rewire'));正在工作,但由于某种原因,它无法运行rewireModule(可能是因为它是ES6+(?这是rewire的index.js:

var rewireModule = require("./rewire.js");
/**
* Adds a special setter and getter to the module located at filename. After the module has been rewired, you can
* call myModule.__set__(name, value) and myModule.__get__(name) to manipulate private variables.
*
* @param {!String} filename Path to the module that shall be rewired. Use it exactly like require().
* @return {*} the rewired module
*/
function rewire(filename) {
return rewireModule(module.parent, filename);
}
module.exports = rewire;
delete require.cache[__filename];   // deleting self from module cache so the parent module is always up to date

这就是我在我的karmaconfig文件中导入的内容,以及我试图测试的文件:

// karma.conf.js
files: [
'./node_modules/rewire/lib/index.js',
'../../public/static/js2/cn-plugin-client-v3.js',
]

我做错什么了吗?或者我只是无法使用ES6之前的限制重新布线。有人测试过没有使用karma/jasmine导出的ES6前JS文件吗?有不同的解决方案吗?

原来没有必要重新布线。将文件添加到karma配置后,您通常可以在测试中通过将其称为模块来实例化:

beforeEach(module('pluginV3'));

这种方法唯一的缺点是你需要满足每一个依赖性。如果pluginV3.js中有一个变量来自pluginV2.js或其他什么,则需要在依赖它的文件之前将其添加到karma文件导入中。

我还没有测试过这么多,但我认为这个解决方案仍然不适合具有不切实际的长依赖链的文件,而且我不知道如何处理具有循环依赖关系的文件。