gpt4 book ai didi

javascript - karma : Can't find variable: exports

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:27 26 4
gpt4 key购买 nike

我写了一个可以同时用于后端和客户端的 Node 模块

(exports || window).Bar= (function () {
return function () { .... }
})();

现在我的 karma 测试使用 PhantomJs 并提示不存在的 exports 变量

gulp.task('test', function () {
var karma = require('karma').server;

karma.start({
autoWatch: false,
browsers: [
'PhantomJS'
],
coverageReporter: {
type: 'lcovonly'
},
frameworks: [
'jasmine'
],
files: [
'bar.js',
'tests/bar.spec.js'
],
junitReporter: {
outputFile: 'target/junit.xml'
},
preprocessors: {
'app/js/!(lib)/**/*.js': 'coverage'
},
reporters: [
'progress',
'junit',
'coverage'
],
singleRun: true
});
});

我得到的错误是

PhantomJS 1.9.7 (Mac OS X) ERROR
ReferenceError: Can't find variable: exports

有没有办法忽略 karam/phantomsJs 中的 exports 变量?

最佳答案

一个常见的模式通常是检查是否定义了exports变量:

(function(){
...
var Bar;
if (typeof exports !== 'undefined') {
Bar = exports;
} else {
Bar = window.Bar = {};
}
})();

此模式用于in Backbone例如 - 好吧,它在源代码中技术上有点复杂,因为它也支持 AMD,但想法就是这样。

您还可以将检查作为包装函数的第一个参数传递给它:

(function(exports){

// your code goes here

exports.Bar = function(){
...
};

})(typeof exports === 'undefined'? this['mymodule']={}: exports);

看看at this blog post了解更多信息。

关于javascript - karma : Can't find variable: exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26192155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com