我的项目是ionic-angularjs-requirejs
我的环境是Mac OS X 10.10.1,节点v0.10.30

我想将 karma 测试集成到我的项目中,但是当我运行grunt test时,这是错误的:

结果:

Running "karma:unit" (karma) task

INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket UhgMIttDejE4Xdm8I7mG with id 52208731
WARN [PhantomJS 1.9.8 (Mac OS X)]: Disconnected (1 times), because no message in 10000 ms.

Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

这是我package.json中的依赖项:
"grunt-bower-requirejs": "^1.1.1",
"grunt-contrib-uglify": "^0.2.7",
"grunt-karma": "^0.9.0",
"karma": "^0.12.28",
"karma-jasmine": "^0.3.2",
"karma-phantomjs-launcher": "^0.1.4",
"karma-requirejs": "^0.2.2",
"requirejs": "^2.1.15"

这是我的Gruntfile.js:
module.exports = function (grunt) {

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    bower: {
        target: {
            rjsConfig: 'js/main.js'
        }
    },
    uglify: {
        options: {
            banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
        },
        build: {
            src: 'src/<%= pkg.name %>.js',
            dest: 'build/<%= pkg.name %>.min.js'
        },
    },
    karma: {
        unit: {
            options: {
                frameworks: ['jasmine', 'requirejs'],
                browsers: ['PhantomJS'],
                autoWatch: true,
                singleRun: true,
                files: [
                    'lib/js/generated/angular/angular.js',
                    'lib/js/generated/angular-mocks/angular-mocks.js',
                    //'js/**/*.js',
                    //'templates/**/*.html',
                    'tests/*.tests.js'
                ],
                exclude: [
                    'js/main.js'
                ]
            }
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-bower-requirejs');
grunt.loadNpmTasks('grunt-karma');

// Tell Grunt what to do when we type "grunt" into the terminal
grunt.registerTask('default', [
    'uglify',
    'bower'
]);
grunt.registerTask('test', [
    'karma'
]);

};

最佳答案

我对grunt-karma插件的经验是,与实际站点相比,我花费更多的时间调试插件。我的解决方案是完全避免使用它,并使用grunt-exec运行我的 karma 命令,或者不使用grunt并在新选项卡中手动运行 karma 。

grunt.loadNpmTasks('grunt-exec');
var config =
    {
      exec:   {
        karma : 'karma start {path to your karma.conf.js file}'
        // or
        // karma : 'karma run {path to your karma.conf.js file}'
      }
    }

我知道这可能不是您要寻找的答案,但是我的经验使我认识到,与原始程序相距仅一步之遥就是您必须调试的其他程序,而这并不是您自己的。

话虽如此,
您的文件似乎没有被包括在内。

我通常将我的 karma 配置文件分解为单独的文件:

config/files.js
module.exports = [
  'public/lib/angular/angular.js',
  'public/lib/lodash/dist/lodash.compat.js',
  'public/lib/jquery/dist/jquery.js',
  'public/lib/angular-ui-router/release/angular-ui-router.js',
  'tests/test-main.js',
  {pattern: 'public/js/**/*.js', included: true},
  {pattern: 'tests/unit/**/*.js', included: true}
];

config/common.conf.js
var files = require('./files.js');

module.exports = {

  // base path that will be used to resolve all patterns (eg. files, exclude)
  basePath: '../',

  frameworks: ['jasmine', 'requirejs'],

  // list of files / patterns to load in the browser
  files: files,


  // list of files to exclude
  exclude: [
  ],

  // web server port
  port: 9876,


  // enable / disable colors in the output (reporters and logs)
  colors: true,

  // enable / disable watching file and executing tests whenever any file changes
  autoWatch: true,

  coverageReporter: {
    type : 'html',
    dir : './tests/coverage/',
    subdir: function(browser) {
      "use strict";
      // normalization process to keep a consistent browser name accross different
      // OS
      return browser.toLowerCase().split(/[ /-]/)[0];
    }
  },

};

config/karma.conf.js

var commonConfig = require('./common.conf.js');
module.exports = function(config) {
  "use strict";
  commonConfig.reporters = ['nyan', 'growl'];
  commonConfig.browsers  = ['PhantomJS'];
  commonConfig.captureTimeout = 60000;
  commonConfig.singleRun = false;
  commonConfig.logLevel  = config.LOG_DEBUG;


  config.set(commonConfig);
};

//    commonConfig.browsers  = ['PhantomJS', 'Chrome']
//    commonConfig.browsers  = ['PhantomJS', 'Chrome', 'Safari', 'Firefox'],

config/build.conf.js
var commonConfig = require('./common.conf.js');

module.exports = function(config) {
  "use strict";
  commonConfig.reporters = ['progress'];
  commonConfig.browsers  = ['PhantomJS'];
  commonConfig.captureTimeout = 120000;
  commonConfig.singleRun = true;
  commonConfig.logLevel  = config.LOG_DEBUG;
  config.set(commonConfig);
};

config/test.coverage.js
var commonConfig = require('./common.conf.js');

module.exports = function(config) {
  "use strict";
  commonConfig.reporters = ['progress','coverage'];
  commonConfig.browsers  = ['PhantomJS', 'Chrome', 'Safari', 'Firefox'];
  commonConfig.captureTimeout = 120000;
  commonConfig.singleRun = true;
  commonConfig.logLevel  = config.LOG_DEBUG;
  commonConfig.preprocessors = {};
  commonConfig.preprocessors['./public/js/*.js'] = ['coverage'];

  config.set(commonConfig);
};

这样做可以让我针对不同的情况运行不同的 karma 配置。

在开发过程中,我使用:
karma 启动config/karma.conf.js

为了在所有浏览器中获得我的测试覆盖率,我需要运行:
karma 启动config/test.coverage.js

并在持续集成过程中验证我的构建是否干净
karma 启动config/build.conf.js

使用此方法,我可以再次使用grunt-exec在适当的时间运行那些命令。

关于angularjs - 警告[PhantomJS 1.9.8(Mac OS X)] : Disconnected (1 times),,因为在10000毫秒内没有消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27310184/

10-13 08:02