Karma(mocha) - 每当单元测试失败时,意外的令牌N.

时间:2015-08-12 08:22:52

标签: javascript unit-testing gulp karma-runner karma-mocha

我正在使用Karma + Mocha来运行我的单元测试,一切都运行良好,除非测试失败, 当我运行像

这样的测试时
expect(player).to.be.an('object');

并且它失败我会期望它说“对象是预期的但给出了字符串”或类似的东西,而是我得到的(对于每一次失败的测试,无论它如何失败,即使我尝试资产如果是,则为true):

SyntaxError: Unexpected token N
        at Object.parse (native)
        at Array.map (native)

我知道我的代码中没有语法错误的事实,所以我猜这与karma / mocha以及他们处理失败的测试的方式有关...我只是不知道在哪里看...这是我的gulp任务:

var karmaServer = require('karma').server;

gulp.task('test', function (done) {
    gutil.log('preparing tests.');
    var runOnlyOnce = true;
    // check if a parameter named "watch" is passed. if so - run tests in watch mode.
    if (argv.watch){
        runOnlyOnce = false;
    }

    if (runOnlyOnce){
        gutil.log('Running only once.\nTo run in "watch" mode try: gulp test --watch');
    } else {
        gutil.log('Running in watch mode. Oh yeah.');
    }

    karmaServer.start({
        configFile: __dirname +'/karma.conf.js',
        singleRun: runOnlyOnce
    }, function(exitCode) {
      gutil.log('Karma has exited with ' + exitCode);
      if (exitCode != 0){
        gutil.log(gutil.colors.bgRed("Test(s) failed."));
      }
      process.exit(exitCode);
    });
});

这是我的karma.conf文件:

module.exports = function (config) {
    'use strict';
    config.set({

        basePath: '',

         frameworks: ['browserify',  'mocha',  'source-map-support'],

        // reporters configuration 
        reporters: ['mocha'],

        preprocessors: {
            'test/**/*.spec.js': ['browserify']
        },

        files: [
            {pattern: 'app/**/*.js', watched: true, included: false, served: false}, // watch these files, but dont bundle them for tests
            'test/**/*.spec.js'
        ],

        browserify: {
            debug: true,
            transform: ['babelify']
        },

        plugins: [
            'karma-mocha-reporter',
            'karma-mocha',
            'karma-phantomjs-launcher',
            'karma-chrome-launcher',
            'karma-browserify',
            'babel-plugin-espower',
            'karma-ie-launcher',
            'karma-source-map-support'
        ],

        port: 9876,
        colors: true,
        usePolling: true,
        atomic_save: false,
        autoWatch : true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,

        browsers: ["Chrome"] //, "IE", 'PhantomJS'

    });
};

任何帮助将不胜感激!!!谢谢!

1 个答案:

答案 0 :(得分:1)

所以我发现了问题,我所做的就是从karma.conf文件中删除调试标志。 而不是

 browserify: {
            debug: true,
            transform: ['babelify']
        },

我做了:

 browserify: {
            debug: false,
            transform: ['babelify']
        },

这就是诀窍。

我希望这对任何人都有帮助!干杯!