如何在不启动任何Jasmine测试的情况下运行Karma服务器

Dyn*_*lon 13 javascript tdd jasmine karma-runner karma-jasmine

我想将--grep=PATTERN标志传递给karma run,以便只执行特定的测试.但是,如果我启动我的业力服务器karma start karma.conf.js,所有Jasmine单元测试立即执行.由于我有几百次测试,这需要太长时间.

问题:如何启动业力服务器并且不运行任何测试,而是在我调用时仅运行测试karma run

SoE*_*zPz 2

听起来您更喜欢的是使用终端通过 karma/jasmine 一次运行一个测试。如果这是正确的,这就是我运行单文件测试的方法。

注意:我使用 gulp 来假脱机 Karma,但这应该仍然适用于仅使用全局 karma。

如果您选择使用的话,这也是我的gulp方法。

gulp.task('karma', function (done) {
  karma.start({
    configFile: __dirname + '/karma.conf.js'
  }, done);
});
Run Code Online (Sandbox Code Playgroud)

在我的karma.config.js中

module.exports = function(config) {

  config.set({

  basePath: '',

  frameworks: ['jasmine'],

  // Note the **setFilesUpForTesting** function below. Just change this.
  files: setFilesUpForTesting(),

  ... the rest of file
} // end of module!!!!!
Run Code Online (Sandbox Code Playgroud)

--> 现在在模块下方的同一个karma.config.js文件中添加以下内容

// ------------------------------------------------------------------ >
// Below code is for running either single or all test files.
// To run a single test, just pass in the test file name like so...

// If the test file name was rs-test-me-now_test.js
// To spool up Karma you would enter

// gulp karma --single-test=rs-test-me-now

// To just run all tests
// gulp karma

var fixedPath = [
  'public/js/jquery.js',
  'public/js/jquery-ui.min.js',
  'public/js/foundation.min.js',
  'public/js/angular.min.js',
  'public/js/angular-mocks.js',
  'public/js/angular-route.min.js',
  'public/js/angular-sanitize.min.js',
  'public/js/angular-cookies.js'
  // all the files you want to include when tests run. I removed most of mine, but left some for reference.
];

var baseTestPath = 'public/test/'; // This is the path from root to your test/spec folder that contains all your tests.

function setFilesUpForTesting(){
  fixedPath.push( testPath());
  return fixedPath;
}

function testPath(){
  return singleTestPath() || fullTestPath();
}

function fullTestPath(){
  // If your root folder was butter, and all your tests were labeled
  // like my_file_spec.js, and all your specs were in a folder under root
  // called bread, then the path below would be
  // 'butter/bread/**/*_spec.js' Got it?
  return  'public/test/**/*_test.js'; // change this to suit your path.
}

function singleTestPath(){
  var passedArgument = process.argv.filter(function(item){ if(/--single-test=/.test(item)){return item; }});
  if( isEmpty( passedArgument )){ return false; }
  return baseTestPath + '**/*' + fileName( passedArgument ) + '_test.js';
  // change above to fit your path.
}

function fileName( argument ){
  if( !argument ){ return; }
  return argument[0].replace('--single-test=', ''); // Change single-test if you want to.
}

function isEmpty( array ){
  return array.length === 0;
}
Run Code Online (Sandbox Code Playgroud)

当我想运行测试时,我只需运行以下命令

对于单文件测试 gulp karma --single-test=test-me-now

// when test-me-now_test.js is the file name.
// and root/pulblic/test/   has all my test files and test folders.
// or root/bread/butter/ and file is called test-me-now_spec.js as in the other example I gave above.
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。如果您无法启动并运行它,请提出问题。

更新

我只是从命令行使用了 karma,并且在使用 gulp 时也按上面所述工作。

如果可以从命令行访问 karma,那么如果您实现上面的 karma.config.js 文件,您现在就可以执行此操作...

注意:我必须使用 karma start而不是run

karma start (will run all tests)
// or for a single test file
karma start --single-test=my-test-file-name // but without the _test.js or _spec.js whatever you call your files. I had reasons for doing this, but you can change the functions above to accept the complete file name if needed.
Run Code Online (Sandbox Code Playgroud)

笔记:

您还可以更改 --single-test= 中“--”后面的措辞,只需确保更新我共享的 karma.config.js 信息即可。