lazd / gulp-karma

Karma plugin for gulp
MIT License
75 stars 36 forks source link

Passing a glob to gulp-karma watches individual files and does not catch newly added files #3

Closed lazd closed 9 years ago

lazd commented 10 years ago

If you pass a glob, say tests/**/*.js, then add files to tests/, they are not included in subsequent test runs as the globs are resolved before being passed to Karma's watch mechanism.

chrisabrams commented 10 years ago

:(

lazd commented 10 years ago

@chrisabrams, I'll be at you soon with a workaround to turn that frown upside down.

dashed commented 10 years ago

Man. I want this so bad.

koblass commented 10 years ago

Why not use the gulp.watch method instead and use the action "run" ? my guess is that it should work

lazd commented 10 years ago

The problem lies in the fact that gulp.src will send a stream of individual File objects, not the actual globs. Karma is doing the actual watching in this case.

@koblass, that would involve an additional task for starting the Karma server, and it was getting a bit ugly when I tried that route before. Will reconsider it, but it's looking like this will end up gulp-friendly and avoid using gulp's src and watch methods completely.

dashed commented 10 years ago

@lazd This may be related to https://github.com/floatdrop/gulp-watch/pull/16#issuecomment-31989717

j0hnsmith commented 10 years ago

+1

lazd commented 10 years ago

Here's what I've found: I can't get the runner to recognize new files if the server has already been started. In order to make it happen, the server needs to be started/killed on every test run.

The following is currently possible:

var gulp = require('gulp');
var karma = require('gulp-karma');

// Include order
// Could be globs instead
var includeOrder = [
  'client/scripts/todo/todo.js',
  'client/scripts/todo/todo.polyfills.js',
  'client/scripts/todo/todo.util.js',
  'client/scripts/todo/todo.App.js'
];

// Add the tests to the include order
// Uses a glob to catch all tests
var testIncludeOrder = includeOrder.concat(['test/client/*.js']);

// Run tests once
gulp.task('test', function() {
  // The glob is evaluated again, so new files are picked up
  return gulp.src(testIncludeOrder)
    .pipe(karma({
      configFile: 'karma.conf.js',
      action: 'run'
    }));
});

gulp.task('default', function() {
  // Run on start
  gulp.run('test');

  // Run on change
  // This watches on the glob, not the individual files
  gulp.watch(testIncludeOrder, function() {
    return gulp.run('test');
  });
});

This is less than ideal. I'll keep looking for a workaround and will investigate into whether Karma can be patched to fix this, but for now, the above workaround does get the job done.

j0hnsmith commented 10 years ago

Idea, probably not a good one: you can set autoWatch : false in karma.conf.js so each time the gulp watch sees a change it invokes karma with a new list of files (from the glob). I haven't tried but this should work however each time karma starts it opens up browsers, which may take a second or two so this might not be very practical.

Just read the previous commend by @lazd, this is more or less the same suggestion.

lazd commented 9 years ago

gulp-karma is deprecated (#43). Please use Karma directly: https://github.com/karma-runner/gulp-karma