获得grunt karma来运行一个单元测试 [英] Getting grunt karma to run one unit test

查看:88
本文介绍了获得grunt karma来运行一个单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人得到咕k因果只能运行一个在手表上更改的规格。这是我的配置下面。问题是行grunt.config('karma.unit.options.files',filepath);似乎没有做任何事情,因为所有规格仍然运行,但是foo在karma之前获得输出:unit:run被解雇。

  grunt.initConfig({
karma:{
单元:{
configFile:'karma.conf.js',
背景:true,
singleRun :false,
options:{
files:allFilesArray
}
}
},
观看:{
options:{
spawn:false,
livereload:true
},
karma:{'js / spec / ** / *。spec.js','js / src / ** / *。js'],
tasks:['karma:unit:run']
}
}
})

grunt.event.on('watch',function(action,filepath){
console.log('foo');
grunt.config('karma.unit.options.files',filepath) ;
});

有没有人在终端上执行文件更改时运行一个规格?我们有成千上万的测试,所以它开始变慢。



谢谢,
Alex

解决方案

我得到了这个工作。基本上,当事件处理程序使用watch时,只要文件发生更改,就会动态更改karma配置。这里是简要介绍:



我的Grunt配置有两个业务任务:全部和一个。 all运行所有这些,one只运行一个事先不知道的文件。

  grunt。 initConfig({
// ...
karma:{
all:{
configFile:'karma.conf.js',
browsersers:['PhantomJS' ],
singleRun:true,
options:{
files:[
'bower_components / jquery / dist / jquery.js',//依赖关系
'src / js / ** / *。js',// js源文件
'src / js / ** / *。spec.js'//单元测试文件
]
}
$ b one:{
configFile:'karma.conf.js',
browsers:['PhantomJS'],
singleRun:true,
files :[
{src:'bower_components / jquery / dist / jquery.js'},//依赖关系
{src:['src / js / ** / *。js','!src / js / ** / *。spec.js']} //源文件
//(排除单元测试文件)
// watchEventListener将添加单元测试文件t o运行
]
}
},
// ...
});

然后在我的grunt文件中添加一个监听事件监听器。该监听器更新业力:一项任务并添加单元测试文件。我们保留原始文件数组的副本,否则我们的添加将持续并累积到监视任务的生命周期中。

  //当单元测试更改时,只执行它
var original_karmaOne_files = grunt.config.get('karma.one.files'); //保留原始文件数组
grunt.event.on('watch',function watchEventListener(action,filepath,target){

//此处理程序处理所有手表更改。从其他监视任务中过滤掉
if(target =='js_spec')handleJSHintSpec();

// ---------------- -----
函数handleJSHintSpec(){
if(action =='deleted')return; //当文件被删除时,我们不需要运行任何测试
/ /这可能会失败,如果一个监视任务一次触发多个文件
//动态更改配置
grunt.config.set('karma.one.files',[] .concat(original_karmaOne_files ,[{src:filepath}]));
}
});

这里是我的gruntfile的监视任务:

  watch:{
// ...
//当js规格文件更改时,
// lint它们
//运行单元测试
js_spec:{
options:{
interrupt:true
},
files:'src / js / ** / *。spec.js',
任务:['jshint:js_spec','karma:one']
},
// ...
}

我的 karma.conf.js 文件是相当默认的,但它的文件数组是空的。实际上,我评论说,所以该属性是未定义的。

  //在浏览器中加载的文件/模式列表
//文件:[],//在gruntfile中指定


I was wondering if anyone has got grunt karma to run just one spec that is changed on watch. This is my config below. The problem is that the line grunt.config('karma.unit.options.files', filepath); doesn't seem to be doing anything as all the specs still get run however foo does get output before the karma:unit:run gets fired.

grunt.initConfig({
    karma: {
        unit: {
            configFile: 'karma.conf.js',
            background: true,
            singleRun: false,
            options: {
                files: allFilesArray
            }
        }
    },
    watch: {
        options: {
            spawn: false,
            livereload: true
        },
        karma: {
            files: ['js/spec/**/*.spec.js', 'js/src/**/*.js'],
            tasks: ['karma:unit:run']
        }
    }
})

grunt.event.on('watch', function (action, filepath){
    console.log('foo');
    grunt.config('karma.unit.options.files', filepath);
});

Is there anyone out there who has achieved running one spec in the terminal on file change? We have thousands of tests so it is starting to get slow.

Thanks, Alex

解决方案

I got this to work. Basically, you use watch with an event handler to dynamically change the karma config whenever a file changes. Here's the rundown:

My Grunt config has two karma tasks: "all" and "one". "all" runs all of them, and "one" only runs a single file which it does not know beforehand.

grunt.initConfig({
  // ...
  karma: {
    all: {
      configFile: 'karma.conf.js',
      browsers: ['PhantomJS'],
      singleRun: true,
      options: {
        files: [
          'bower_components/jquery/dist/jquery.js', // dependencies
          'src/js/**/*.js', // js source files
          'src/js/**/*.spec.js' // unit test files
        ]
      }
    },
    one: {
      configFile: 'karma.conf.js',
      browsers: ['PhantomJS'],
      singleRun: true,
      files: [
        {src: 'bower_components/jquery/dist/jquery.js'}, // dependencies
        {src: ['src/js/**/*.js','!src/js/**/*.spec.js']} // source files
        // (exclude the unit test files)
        // watchEventListener will add the unit test file to run
      ]
    }
  },
  // ...
});

And then later in my gruntfile, I add a listener for watch events. This listener updates the karma:one task and adds the unit test file. We keep a copy of the original files array, or else our additions would persist and accumulate through the lifetime of the watch task.

// when a unit test changes, execute only it
var original_karmaOne_files = grunt.config.get('karma.one.files'); // keep the original files array
grunt.event.on('watch', function watchEventListener(action, filepath, target){

  // this handler handles ALL watch changes. Try to filter out ones from other watch tasks
  if (target == 'js_spec') handleJSHintSpec();

  // ---------------------
  function handleJSHintSpec() {
    if (action == 'deleted') return; // we don't need to run any tests when a file is deleted
    // this will probably fail if a watch task is triggered with multiple files at once
    // dynamically change the config
    grunt.config.set('karma.one.files', [].concat(original_karmaOne_files, [{src: filepath}]));
  }
});

And here is my gruntfile's watch task:

watch: {
  // ...
  // when js spec files change,
  // lint them
  // run unit tests
  js_spec: {
    options: {
      interrupt: true
    },
    files: 'src/js/**/*.spec.js',
    tasks: ['jshint:js_spec', 'karma:one']
  },
  // ...
}

My karma.conf.js file is pretty default, but its files array is empty. Actually, I commented it out, so the property is undefined.

// list of files / patterns to load in the browser
//files: [], // specified in the gruntfile

这篇关于获得grunt karma来运行一个单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆