Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Use a gulp watch #21

Closed
mikehaas763 opened this issue Mar 12, 2014 · 17 comments
Closed

Use a gulp watch #21

mikehaas763 opened this issue Mar 12, 2014 · 17 comments

Comments

@mikehaas763
Copy link

Hi. I know you have just depended on using karma's builtin file-watching with this plugin thus far to keep it easy. I'm interested in getting it to use one of the gulp watch only plugins though. Either the built-in gulp.watch or the gulp-watch plugin.

A few reasons why I think this is important:

  • Gulp's philosophy is using plugins that do one thing and do it really well.
  • No need to have more than one watcher which I think would be good for performance.
  • Grunt already does this.

I haven't even looked in to what this would take. In any case, would you be open to it?

@lazd
Copy link
Owner

lazd commented Mar 12, 2014

I've had trouble passing a new list of files to an already running Karma server. See my comment here #3 (comment) for a detailed description and an example that uses gulp's watch mechanism (but doesn't leave the server running).

Maybe this is fixed in Karma 0.12, I haven't had time to look into it. @mikehaas763, can you investigate and send a pull request with a solution if one exists?

@mikehaas763
Copy link
Author

@lazd I think I will!

@demisx
Copy link

demisx commented Mar 27, 2014

I like the idea of using one watcher. Makes sense. @mikehaas763 have you had a chance to investigate?

@mikehaas763
Copy link
Author

I did investigate a bit and found that it needs to spawn a child process in the bg just like grunt-karma does.

@lazd
Copy link
Owner

lazd commented Apr 18, 2014

@mikehaas763 and @demisx, with a few downright filthy hacks, I've put together a branch that repurposes gulp-karma into a sort of helper (similar to node-karma-wrapper, but more aware of what's going on inside of Karma). It's made to be used with gulp's built-in watch and doesn't suffer from any of the issues filed against this repository.

https://github.com/lazd/gulp-karma/tree/helper

However, it is no longer a gulp plugin because it doesn't deal with streams of files. In fact, it has nothing to do with gulp except for that fact that it's probably the only sane way I've seen to use Karma and gulp together.

I've created a test repository that employs the helper branch to test a silly little todo app:

https://github.com/lazd/gulp-karma-test

The promise-based implementation of the helper is very digestible:

var gulp = require('gulp');

// Create a gulp+karma helper with a set of default options
// In this case, the options all come from the config file
var karma = require('gulp-karma')({
  configFile: 'karma.conf.js'
});

// Run tests once
gulp.task('test', function() {
  // Override configuration for CI, etc
  return karma.once({
    reporters: ['coverage']
  });
});

// WATCH OPTION 1: gulp.watch style
gulp.task('gulp-watch', function() {
  // Start a server, then, once it's ready, run tests
  karma.start().then(karma.run);

  // Watch for changes with gulp and run tests accordingly
  gulp.watch(['client/scripts/todo/*.js', 'test/client/*.js'], function() {
    karma.run();
  });
});

// WATCH OPTION 2: Karma autoWatch style
gulp.task('karma-watch', function() {
  // Start a karma server, run tests, then watch with karma
  return karma.start({
    autoWatch: true
  });
});

This is great, but this issue karma-runner/karma#1037 is the reason for the aforementioned downright filthy hacks -- Karma does't lend itself to being programmatically controlled in this fashion. Once that issue is addressed in a robust way, this kind of "gulp helper" will likely be the best way to use gulp with Karma.

But wait! Why do we need a gulp plugin for Karma again? Why didn't I just use Karma's public API like @vojtajina suggested? It. just. won't. die. See karma-runner/karma#1035 . Also, it's nice to avoid repeating yourself and passing the configuration around. And promises are fun.

@allansson
Copy link

@lazd I think the direction you are taking with the library is great, since the clash between karma and gulp has caused nothing but trouble for me so far. However, I have an idea on how you could make the helper more of proper gulp-plugin: let the gulp.src point to the karma configuraiton file.

gulp.task('test', function () {
    return gulp.src('karma.conf.js')
        .pipe(karma.once({
            reporters: ['coverage']
    }));
});

What do you think?

@mmoulton
Copy link

+1 for @allansson idea to keep this plugin idiomatic to gulp and streams.

@lazd
Copy link
Owner

lazd commented Apr 24, 2014

@mmoulton it would be even more idiomatic to gulp if it didn't exist. Please checkout the helper branch and the associated Karma issues and give your feedback on that.

@Keats
Copy link

Keats commented Apr 25, 2014

Just checked out that branch, option 2 looks really nice.
Is it able to handle errors gracefully when using .once ?

@lazd
Copy link
Owner

lazd commented Apr 26, 2014

@Keats, I haven't throughly vetted all of the error handling, but with @chrisrhoden's PR #29, it looks like it should handle errors nicely.

@Keats
Copy link

Keats commented Apr 27, 2014

For example I have a CI task that runs protractor and karma tasks at the same time, with karma failing and protractor succeeding, the task will return 0 (https://travis-ci.org/Cookingenius/Plate/builds/23863259) but it's doing that for the current version too so nevermind !
For some reasons I was sure it was returning an error.

I think you should push it as 0.1 as it's much better than the current one.

@cqr
Copy link

cqr commented Apr 27, 2014

Looks like a gulp bug. You can clearly see that it reports the error to
gulp.

[gulp] 'karma' errored after 2.8 s {"code":1}
On Apr 27, 2014 9:02 AM, "Vincent Prouillet" notifications@github.com
wrote:

For example I have a CI task that runs protractor and karma tasks at the
same time, with karma failing and protractor succeeding, the task will
return 0 (https://travis-ci.org/Cookingenius/Plate/builds/23863259) but
it's doing that for the current version too so nevermind !
For some reasons I was sure it was returning an error.

I think you should push it as 0.1 as it's much better than the current one.


Reply to this email directly or view it on GitHubhttps://github.com//issues/21#issuecomment-41495982
.

@Keats
Copy link

Keats commented May 3, 2014

@lazd Are you planning to publish that branch to npm?
People can still use 0.4 if they prefer the other way.

@lazd
Copy link
Owner

lazd commented May 17, 2014

@Keats, I won't be publishing this until the referenced Karma issue is closed. You can use this branch in your package.json until then, however. See https://www.npmjs.org/doc/json.html#Git-URLs-as-Dependencies

@jonira
Copy link

jonira commented Jun 10, 2014

Any progress on this one?

@lazd
Copy link
Owner

lazd commented Jun 10, 2014

@JRajan see karma-runner/karma#1037 for progress.

@lazd
Copy link
Owner

lazd commented Oct 1, 2015

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

@lazd lazd closed this as completed Oct 1, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants