【问题标题】:Karma (mocha) - Unexpected token N whenever unit test failsKarma (mocha) - 单元测试失败时出现意外的令牌 N
【发布时间】:2015-08-12 08:22:52
【问题描述】:

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

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

它失败了,我希望它说“预期对象,但给出了字符串”或类似的东西,相反,我得到的只是(对于每一个失败的测试,无论它如何失败,即使我尝试资产真假):

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

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

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'

    });
};

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

【问题讨论】:

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


    【解决方案1】:

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

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

    我做到了:

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

    成功了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多