karma-runner / grunt-karma

Grunt plugin for Karma.
MIT License
468 stars 116 forks source link

Race condition with browserify preprocessing? #127

Open levity opened 10 years ago

levity commented 10 years ago

I'm using karma-bro to provide browserify support for karma, and it looks like there's a race condition between the preprocessing and the running of tests. It only happens with grunt-karma, not when I run karma directly.

When I change one of my test files to flip a test between failing and not failing, then save, watch picks up the change, but the test outcome is as if I hadn't made the change. When I make and save a second change, the outcome is as if I'd only made the first change. e.g.:

it('does basic math', function() { expect(4 + 4).toEqual(8); }); 
// file starts like this

it('does basic math', function() { expect(4 + 4).toEqual(10); }); 
// save -- test passes

it('does basic math', function() { expect(4 + 4).toEqual(9); }); 
// save -- test fails with "expected 8 to equal 9"

it('does basic math', function() { expect(4 + 4).toEqual(8); }); 
// save -- test fails with "expected 8 to equal 10"

I'm guessing the preprocessing is happening in the karma:dev:start thread, so there's a race condition. Any suggestions for how to work around this?

My Karma config:

'use strict';

module.exports = function(config) {
  config.set({
    basePath: '',
    browserify: {
      debug: true,
      transform: ['debowerify']
    },
    frameworks: ['browserify', 'jasmine'],
    files: [
      'test/setup.js',
      'test/**/*.tests.js'
    ],
    preprocessors: {
      'test/**/*.js': ['browserify']
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

My Grunt config (excerpted):

module.exports = function(grunt) {

  grunt.initConfig({
    karma: {
      options: {
        configFile: 'karma.config.js',
      },
      dev: {
        autoWatch: false,
        background: true,
        singleRun: false,
        logLevel: 'DEBUG'
      },
      once: {
        singleRun: true,
        browsers: ['PhantomJS']
      }
    },
    watch: {
      karma: {
        files: ['src/**/*', 'test/**/*.js'],
        tasks: ['karma:dev:run']
      }
    }
  });

  require('load-grunt-tasks')(grunt);
  grunt.registerTask('dev', ['karma:dev:start', 'watch']);
};
mcoquet commented 9 years ago

have you had any success with this? I'm suffering from the same issue.

levity commented 9 years ago

Sorry, I haven't.

On Mon, Mar 23, 2015 at 7:04 AM, Miguel Coquet notifications@github.com wrote:

have you had any success with this? I'm suffering from the same issue.

— Reply to this email directly or view it on GitHub https://github.com/karma-runner/grunt-karma/issues/127#issuecomment-85011579 .