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

标签 javascript jasmine tdd karma-runner karma-jasmine

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

问题:当我调用karma run 时,如何启动 karma 服务器而不运行任何测试,而是只运行测试?

最佳答案

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

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

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

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

在我的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!!!!!

--> 现在在模块下方,在同一个 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;
}

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

For single file tests 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.

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

更新

我只是在命令行中使用了 karma,并且在使用 gulp 时也像上面所说的那样工作。

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

注意:我必须使用 karma 开始而不是运行

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.

注意:

你也可以改变--single-test=中“--”后面的写法 只需确保更新我共享的 karma.config.js 信息即可。

关于javascript - 如何在不启动任何 Jasmine 测试的情况下运行 Karma 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197745/

相关文章:

javascript - 使用 Jquery 调用 Javascript 函数

typescript - 如何将 'describe' 、 'expect' 和 'it' 导入 IDE 的 typescript 测试中以不突出显示它们

rspec - BDD 循环 - 如何加入后端与前端

unit-testing - Jasmine:每个 "it"一个匹配器还是多个?

python pytest 用于测试请求和响应

javascript - 使用 Javascript 单击时获取链接的 ID

javascript - 在 JavaScript 中,为什么 "0"等于 false,但是当通过 'if' 测试时,它本身不是 false?

unit-testing - 在维护测试套件时,所有 'errors' 最终都应该变成 'failures' 吗?

c# - NUnit 测试夹具层次结构

javascript - 使用jquery读取wikipedia url的内容,跨域网络调用