用grunt-karma-sonar进行unit testing

我正在尝试使用Grunt,Karma和Sonar进行unit testing和testing覆盖。 我正在通过这个教程 。

这是我的依赖:

"devDependencies": { "bower": "~1.3.1", "grunt": "~0.4.4", "grunt-autoprefixer": "~2.0.0", "grunt-cli": "~0.1.13", "grunt-contrib-jasmine": "~0.6.1", "grunt-karma": "~0.8.0", "grunt-karma-sonar": "~0.1.3", "istanbul": "~0.4.2", "jasmine-core": "~2.3.4", "karma": "~0.12.0", "karma-chrome-launcher": "~0.1.12", "karma-coverage": "~0.2.0", "karma-firefox-launcher": "~0.1.6", "karma-jasmine": "~0.2.1", "karma-junit-reporter": "~0.2.1", "karma-phantomjs-launcher": "~0.1.2", "karma-junit-sonarqube-reporter": "0.0.1", "load-grunt-tasks": "^3.4.0", "phantomjs": "~1.9.19", } 

这是我的gruntfile:

 //require('load-grunt-tasks')(grunt); module.exports = function (grunt) { var $srcFiles = 'taf_table_view/js/*.js'; var $testFiles = 'test/*.js'; var $outputDir = 'test_results' var $junitResults = $outputDir + '/junit-test-results.xml'; var $jasmineSpecRunner = $outputDir + '/_SpecRunner.html'; var $coverageOutputDir = $outputDir + '/coverage'; grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // Read project settings from package.json // Jasmine test jasmine: { pivotal: { src: [ // load dependencies first and make sure angular 'taf_table_view/lib/angular/angular.js', 'taf_table_view/lib/angular-route/angular-route.js', 'taf_table_view/lib/angular-mocks/angular-mocks.js', 'taf_table_view/lib/angular-loader/angular-loader.js', 'taf_table_view/lib/angular-animate/angular-animate.js', // load taf_table_view and test scripts 'taf_table_view/js/*.js', 'http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js', 'test/*.js' ], options: { specs: $testFiles, outfile: $jasmineSpecRunner, keepRunner: 'true' // keep SpecRunner/outfile file } } }, karma: { // documentation: https://github.com/karma-runner/grunt-karma plugins : [ 'karma-chrome-launcher', 'karma-firefox-launcher', 'karma-phantomjs-launcher', 'karma-jasmine', // needed for sonar plugin 'karma-junit-reporter', 'karma-coverage' ], continuous: { singleRun: 'true', browsers: [ 'PhantomJS' ] // PhantomJS }, options: { configFile: 'karma.conf.js', }, unit: { singleRun: true, reporters: ['junit', 'coverage'] } //continuous: { // background: true //} }, karma_sonar: { // documentation: https://github.com/mdasberg/grunt-karma-sonar // http://ahexamples.blogspot.com/2014/03/example-of-jasmine-karma-sonar-grunt.html options: { defaultOutputDir: '/sonar/', instance: { // http://localhost:9000 // http://10.207.120.229:9000 hostUrl: 'http://localhost:9000', // jdbc:h2:tcp://localhost:9092/sonar // jdbc:postgresql://10.207.120.229:5432/sonar jdbcUrl: 'jdbc:h2:tcp://localhost:9092/sonar', login: 'sonar', // sonar password: 'sonar1dev' } }, update_sonar: { project: { // Project information shown in sonar key: 'taf-table-view', name: 'TAF Table View', version: '0.0.1' } // sources set at runtime } } }); grunt.registerTask('set-karma-sonar-sources-property', function() { var $done = this.async(); var $phantomjs = require('karma-phantomjs-launcher/node_modules/phantomjs'); var $spawn = require('child_process').spawn; var $phantomUserAgent = $spawn($phantomjs.path, // phantomjs script to print user agent string [ 'lib/phantomjs-useragent.js' ] ); /* * Construct coverage LCOV file path from PHantomJs' * user agent string, then use it to set karmaSonar's * sources property. */ $phantomUserAgent.stdout.on('data', function(msg) { var $useragent = require('karma/node_modules/useragent'); var $agent = $useragent.parse(msg); // An example of dirName is 'PhantomJS 1.9.7 (Mac OS X)' var $dirName = $agent.toAgent() + ' (' + $agent.os + ')'; var $coverageResults = $coverageOutputDir + '/' + $dirName + '/lcov.info'; var $sonarSources = makeSonarSourceDirs($srcFiles, $coverageResults); var $karmaSonarConfig = 'karmaSonar'; var $ksConfig = grunt.config($karmaSonarConfig); grunt.log.writeln('coverage LCOV file: ' + $coverageResults); $ksConfig['your_target']['sources'] = $sonarSources; grunt.config($karmaSonarConfig, $ksConfig); }); $phantomUserAgent.on('close', function(exitCode) { $done(); }); /* * Create sonar source object for each directory of source file pattern. */ function makeSonarSourceDirs($filesPattern, $coverageResults) { var $path = require('path'); var $dirs = []; grunt.file.expand( { filter: function($filePath) { $dirs.push({ path: $path.dirname($filePath), prefix: '.', // path prefix in lcov.info coverageReport: $coverageResults, testReport: $junitResults }); } }, $filesPattern ); return $dirs; } }); // Tell Grunt we plan to use these plug-ins grunt.loadNpmTasks('grunt-contrib-jasmine'); grunt.loadNpmTasks('grunt-karma'); grunt.loadNpmTasks('grunt-karma-sonar'); grunt.loadNpmTasks('load-grunt-tasks'); grunt.registerTask('test', [ 'jasmine', 'karma:continuous' ]); grunt.registerTask('sonar-only', [ 'set-karma-sonar-sources-property', 'karma_sonar:update_sonar' ]); grunt.registerTask('sonar', [ 'test', 'sonar-only' ]); grunt.registerTask('default', 'test'); 

这是我运行grunt sonar时得到的错误:

运行“set-karma-sonar-sources-property”任务警告:找不到模块“karma-phantomjs-launcher / node_modules / phantomjs”使用–force继续。

       

网上收集的解决方案 "用grunt-karma-sonar进行unit testing"