获得咕噜咕噜的业力来进行一次单元测试

ale*_*ers 11 gruntjs karma-runner

我想知道是否有人有咕噜咕噜的业力来运行一个在观看时改变的规格.这是我的配置如下.问题是行grunt.config('karma.unit.options.files',filepath); 似乎没有做任何事情,因为所有的规格仍然运行,但foo确实在业力之前获得输出:unit:run被解雇.

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);
});
Run Code Online (Sandbox Code Playgroud)

是否有人在文件更改中在终端中运行了一个规范?我们有成千上万的测试,所以它开始变慢.

谢谢,亚历克斯

Mat*_*ley 7

我得到了这个工作.基本上,您使用带有事件处理程序的watch来在文件更改时动态更改karma配置.这是破败的:

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

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
      ]
    }
  },
  // ...
});
Run Code Online (Sandbox Code Playgroud)

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

// 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}]));
  }
});
Run Code Online (Sandbox Code Playgroud)

这是我的gruntfile的监视任务:

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']
  },
  // ...
}
Run Code Online (Sandbox Code Playgroud)

我的karma.conf.js文件非常默认,但其文件数组为空.实际上,我对它进行了评论,因此该属性未定义.

// list of files / patterns to load in the browser
//files: [], // specified in the gruntfile
Run Code Online (Sandbox Code Playgroud)

  • 另外,我发现files数组必须包含带有`src`属性的对象 - 不是简单的文件路径字符串,否则它就失败了. (2认同)