Alex King
Alex King

Reputation: 200

grunt karma coverage fails on jenkins but works on windows/unix

I have a node project which is built using grunt and browserify, and tested using karma and jasmine using browserify-istanbul for code coverage.

The task works fine on both windows and linux, however it fails when running on jenkins due to browserify.

Running "karma:coverage" (karma) task
31 01 2017 11:38:48.891:ERROR [framework.browserify]: bundle error
31 01 2017 11:38:48.892:ERROR [framework.browserify]: Error: Line 2:      Unexpected token : while parsing file: /a/path/to/a/file.json
31 01 2017 11:38:48.912:INFO [karma]: Karma v1.4.1 server started at http://0.0.0.0:9876/
31 01 2017 11:38:48.913:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
31 01 2017 11:38:48.917:ERROR [framework.browserify]: bundle error
31 01 2017 11:38:48.918:ERROR [framework.browserify]: Error: Line 2: Unexpected token : while parsing file: /a/path/to/another/file.json
31 01 2017 11:38:48.922:INFO [launcher]: Starting browser PhantomJS
31 01 2017 11:38:49.561:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket hd7llziNhpmzkjibAAAB with id 81120883
Warning: Task "karma:coverage" failed. Use --force to continue.

The karma config for the coverage task is as follows:

module.exports = function( config ) {
  config.set({
    basePath: '..',
    frameworks: [ 'browserify', 'jasmine' ],
    files: [
      // load dependencies here
      { pattern: 'test/**/*.spec.js', watched: false, included: true, served: true },
      { pattern: 'spec/**/*.json', watched: true, served: true, included: false },
      { pattern: 'spec/**/*.xml', watched: true, served: true, included: false },
      { pattern: 'spec/**/*.html', watched: true, included: false, served: true },
    ],
    preprocessors: {
      'test/**/*.spec.js': [ 'browserify' ]
    },
    reporters: [ 'coverage' ],
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: [ 'PhantomJS' ],
    browserify: {
      debug: true,
      transform: [ 'browserify-shim', 'hbsfy', 'brfs', 'browserify-istanbul', {
        ignore: [
          '**/test/**',
          '**/*.hbs',
          '**/*.json',
          '**/js/shims/*.js',
        ],
      },
    ]},
    coverageReporter = {
      dir: 'coverage',
      reporters: [
        {type: 'text'},
        {type: 'html', subdir: '.'},
        {type: 'cobertura', subdir: '.'}
      ]
    },
    plugins: [
      'karma-phantomjs-launcher',
      'karma-jasmine',
      'karma-browserify',
      'karma-mocha-reporter'
      'karma-coverage',
    ],
    singleRun: true
  });
 };

I have other projects which work fine with this configuration which don't have json files. Is there any reason why browserify fails on the *.json files on Jenkins, but works fine on Windows/Linux. Otherwise are there any other red flags here?

Could the "browserify-istanbul" transform be the culprit here?

Upvotes: 1

Views: 414

Answers (1)

SirPeople
SirPeople

Reputation: 4348

Maybe I am a bit late but this would solve the life for few people like me that struggle with this problem.

If we take a look at browserify-istanbul we can observe this function:

function shouldIgnoreFile(file, options) {
  var ignore = options.defaultIgnore === false ? [] : defaultIgnore;
  ignore = ignore.concat(options.ignore || []);

  return ignore.some(function(pattern) {
    return minimatch(file, pattern, options.minimatchOptions);
  });
}

My first guess was that, for some reason, the default ignore was being ignore, so I force it... but the problem persist.

The second and only possibility left was that minimatch was doing trickery... and I was correct, I can bet that in your bundle error path: /a/path/to/a/file.json, there is a directory that starts with ., like .jenkins/test-enviroment/src/.../file.json, and here is where the problem lies. In your local enviroment you dont need to check the path for directories or files that start with a dot, but in the jenkins directory, this .directory is added to the path, making the wildcard pattern checker fail.

To fix it, is as easy as to pass the option minimatchOptions: {dot: true} to browserify-istanbul.

browserify: { debug: true, transform: [ 'browserify-shim', 'hbsfy', 'brfs', 'browserify-istanbul', { ignore: [ '**/test/**', '**/*.hbs', '**/*.json', '**/js/shims/*.js', ], minimatchOptions: {dot: true} }, ]}

Upvotes: 1

Related Questions