fadeys.work
fadeys.work

Reputation: 499

Karma (mocha) - Unexpected token N whenever unit test fails

I'm using Karma + Mocha to run my unit tests, everything works pretty well except whenever the tests fails, When I run a test like

expect(player).to.be.an('object');

and it fails I would expect it to say that "Object was expected but string given" or something like that, Instead all I get is (for every single failed test, no matter how it fails, even when I try to asset true with false):

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

I know for a fact that there's no syntax errors in my code, so Im guessing that's something to do with karma/mocha and the way they handle the failed tests.. I just dont know where to look.. here is my gulp task:

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);
    });
});

Here is my karma.conf file:

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'

    });
};

Any help would be much appreciated!!! Thank you!

Upvotes: 1

Views: 496

Answers (1)

fadeys.work
fadeys.work

Reputation: 499

So I found the problem, all I did was to remove the debug flag from the karma.conf file.. instead of

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

I did:

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

that did the trick.

I hope this helps anyone! cheers!

Upvotes: 1

Related Questions