2014-12-22 20 views
2

在開發過程中,我使用karma和grunt監視文件更改並運行測試。獲取karma服務器在grunt手錶上啓動

在命令行中,我想能夠簡單地輸入

$ grunt watch 

,並有善緣服務器再次啓動,之後有咕嚕留意變化和運行的各種任務(包括業力測試)每當文件改變。我不想輸入$ karma start

這是如何實現的?

回答

6

選項#1

一個可以使用的grunt-contrib-watchatBegin選項。我們的想法是引進啓動任務,這將在觀察者的啓動運行:

watch: { 
    startup: { 
     files: [], // This is redundant, but we get an error if not specifying files. 
     tasks: [ 'karma:continuous:start' ], 
     options: { 
      atBegin: true, 
      spawn: false 
     } 
    }, 

    ... 
} 

Gruntfile.js

module.exports = function(grunt) { 

    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 

     karma: { 
      options: { 
       files:  [ 'client/**/*.spec.js' ], 
       frameworks: [ 'jasmine' ], 
       reporters: [ 'progress' ], 
       browsers: [ 'PhantomJS' ], 
       singleRun: true, 
       autoWatch: false 
      }, 
      continuous: { 
       singleRun: false, 
       background: true 
      } 
     }, 

     concat: { ... }, 

     uglify: { ... }, 

     watch: { 
      startup: { 
       files: [], // This is redundant, but we get an error if not specifying files. 
       tasks: [ 'karma:continuous:start' ], 
       options: { 
        atBegin: true, 
        spawn: false 
       } 
      }, 

      js: { 
       files: [ '<%= concat.js.src %>' ], 
       tasks: [ 'concat:js', 'uglify' ] 
      }, 

      karma: { 
       files: [ '<%= concat.js.src %>', 'src/**/test/**/*.js' ], 
       tasks: [ 'karma:continuous:run' ] 
      }, 

     } 
    }); 

    require('load-grunt-tasks')(grunt); 

    grunt.registerTask('default', [ 'concat', 'uglify', 'karma:unit:run' ]); 
}; 

選項#2

thisthis所示博客,替代方案是代替呼叫

$ grunt watch 

一個創建啓動因緣服務器另一個任務:

grunt.registerTask('serve', [ 'karma:continuous:start', 'watch' ]); 

,然後調用:

$ grunt serve 

Gruntfile.js

module.exports = function(grunt) { 

    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 

     karma: { 
      options: { 
       configFile: 'karma.conf.js' 
      }, 
      unit: { 
       singleRun: true 
      }, 
      continuous: { 
       // keep karma running in the background 
       background: true 
      } 
     }, 

     concat: { ... }, 

     uglify: { ... }, 

     watch: { 
      js: { 
       files: [ '<%= concat.js.src %>' ], 
       tasks: [ 'concat:js', 'uglify' ] 
      }, 

      karma: { 
       files: [ '<%= concat.js.src %>', 'src/**/test/**/*.js' ], 
       tasks: [ 'karma:continuous:run' ] 
      }, 

     } 
    }); 

    require('load-grunt-tasks')(grunt); 

    grunt.registerTask('default', [ 'concat', 'uglify', 'karma:unit:run' ]); 

    grunt.registerTask('serve', [ 'karma:continuous:start', 'watch' ]); 
}; 
+0

我應該避免像 「謝謝」 的意見,但。 .. 謝謝!! – demonkoryu

相關問題