Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Karma测试单个模块_Javascript_Unit Testing_Gulp_Karma Runner_Gulp Karma - Fatal编程技术网

Javascript 使用Karma测试单个模块

Javascript 使用Karma测试单个模块,javascript,unit-testing,gulp,karma-runner,gulp-karma,Javascript,Unit Testing,Gulp,Karma Runner,Gulp Karma,我的web应用程序中很少有独立的模块,我想单独测试它们。可能与业力有关吗 详细信息 有gulpfile.js和karma.conf.js的部分内容: //gulpfile.js gulp.task('test', function (done) { new karmaServer({ configFile: __dirname + '/source/__test__/karma.conf.js', singleRun: true }, done).start(); });

我的web应用程序中很少有独立的模块,我想单独测试它们。可能与业力有关吗

详细信息

gulpfile.js
karma.conf.js
的部分内容:

//gulpfile.js
gulp.task('test', function (done) {
  new karmaServer({
    configFile: __dirname + '/source/__test__/karma.conf.js',
    singleRun: true
  }, done).start();
});

//karma.conf.js
module.exports = function(config) {
  config.set({
    browsers: ['Chrome', 'Firefox'],
    frameworks: ['mocha', 'chai'],
    basePath: '.',
    files: [
      '../js/**/*.js',
      './**/*.spec.js'
    ]
  });
};
提供配置构建karma服务器,为
./js
目录中的所有
.*.js
文件和
.*.spec.js
目录中的所有
.
文件提供服务。这几乎没什么问题,只是所有模块都在一个浏览器实例中模拟加载,就像它们在应用程序中工作一样。我想要的是允许我只为一个模块创建测试,并只加载给定模块所需的文件。Smth是这样的:

//hello.js
function greet(name) {
  return 'Hello, ' + name + '!';
}

//hello.specs.js
load('../hello.js'); // I'm going to test it only. Let browser load it 
describe('greeter', function () {
  it('should say Hello to the World', function () {
    expect(greet('World')).to.equal('Hello, World!');
  });
});

我知道可以为每个模块创建一个吞咽任务,但是我有很多小的
.js
文件,看起来单独测试它们会更容易。我是否以错误的方式使用业力?或者也许我错过了什么

我也有同样的问题。我用
文件加载我的所有模块文件:[my/base/appfolder/***/.js,my/base/testfolder/***-test.js]
,但当karma运行时(所有这些文件),测试文件会相互冲突,例如在声明相同的全局变量进行模拟时。如何告诉karma为每个testjs文件(或每个父级
描述(…)
)运行另一个浏览器或reinit DOM。