单元测试 – 无法在PhantomJS浏览器中执行karma单元测试

前端之家收集整理的这篇文章主要介绍了单元测试 – 无法在PhantomJS浏览器中执行karma单元测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在grunt中,我们通过以下方式在PhantomJs中执行单元测试.
  1. karma: {
  2. options: {
  3. configFile: 'karma.conf.js',runnerPort: 9876,browsers: ['Chrome']
  4. },unitTestDev: {
  5. browsers: ['PhantomJS'],singleRun: true
  6. }
  7. }
  8.  
  9. grunt.registerTask('unitTest',['karma:unitTestDev']);

这是karma.conf.js文件.

  1. // Karma configuration
  2. // Generated on Thu Apr 03 2014 13:44:39 GMT-0500 (Central Daylight Time)
  3.  
  4. module.exports = function(config) {
  5. config.set({
  6.  
  7. // base path that will be used to resolve all patterns (eg. files,exclude)
  8. basePath: '',// frameworks to use
  9. // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  10. frameworks: ['jasmine'],// list of files / patterns to load in the browser
  11. files: [
  12. {pattern: 'dist/lib/jquery/dist/jquery.js',watched: false,served: true,included: true},{pattern: 'dist/lib/jasmine-jquery/lib/jasmine-jquery.js',{pattern: 'dist/lib/angular/angular.js',{pattern: 'dist/lib/angular-route/angular-route.min.js',{pattern: 'dist/lib/angular-mocks/angular-mocks.js',{pattern: 'dist/main.min.js',{pattern: 'public/test/unit/**/*Spec.js',// fixtures
  13. {pattern: 'public/test/mock/**/*.json',watched: true,included: false}
  14. ],// list of files to exclude
  15. exclude: [
  16.  
  17. ],// preprocess matching files before serving them to the browser
  18. // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
  19. preprocessors: {
  20.  
  21. },// test results reporter to use
  22. // possible values: 'dots','progress'
  23. // available reporters: https://npmjs.org/browse/keyword/karma-reporter
  24. reporters: ['progress','html','junit','story'],htmlReporter: {
  25. outputDir: 'build/unit-test/html-report',templatePath: 'node_modules/karma-html-reporter/jasmine_template.html'
  26. },junitReporter: {
  27. outputFile: 'build/unit-test/junit-report/test-results.xml',suite: ''
  28. },// web server port
  29. port: 9876,// enable / disable colors in the output (reporters and logs)
  30. colors: true,// level of logging
  31. // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
  32. logLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changes
  33. autoWatch: false,// start these browsers
  34. // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  35. browsers: ['Chrome'],// Continuous Integration mode
  36. // if true,Karma captures browsers,runs the tests and exits
  37. singleRun: false
  38. });
  39. };

执行“karma:unitTestDev”命令时,会出现以下错误.

  1. Running "karma:unitTestDev" (karma) task
  2. INFO [karma]: Karma v0.11.14 server started at http://localhost:9876/
  3. INFO [launcher]: Starting browser PhantomJS
  4. ERROR [launcher]: Cannot start PhantomJS
  5. Error: spawn OK
  6. INFO [launcher]: Trying to start PhantomJS again (1/2).
  7. ERROR [launcher]: Cannot start PhantomJS
  8. Error: spawn OK
  9. INFO [launcher]: Trying to start PhantomJS again (2/2).
  10. ERROR [launcher]: Cannot start PhantomJS
  11. Error: spawn OK
  12. ERROR [launcher]: PhantomJS Failed 2 times (cannot start). Giving up.
  13. Warning: Task "karma:unitTestDev" Failed. Use --force to continue.
  14.  
  15. Aborted due to warnings.

我们在Windows和Linux OS中遇到了上述问题.在Mac中,业力成功地在PhantomJS上执行测试.

在Windows中,如果我们在Chrome浏览器中执行上述测试,而不是PhantomJS,那么karma会成功执行所有单元测试.

以下是Package.json中与业力相关的依赖关系

  1. "phantomjs": "~1.9.7-3","karma": "~0.12.3","karma-chrome-launcher": "~0.1.3","karma-jasmine": "~0.1.5","karma-html-reporter": "~0.2.3","karma-junit-reporter": "~0.2.1","grunt-karma": "~0.8.2","karma-phantomjs-launcher": "~0.1.3"

让我知道是否有与PhantomJS的业力或咕噜声有任何已知问题.

解决方法

“Karma-phantomjs-launcher”v0.1.3存在问题.将其更改为v0.1.4,然后它可以正常工作.

猜你在找的JavaScript相关文章