gulp-karma-runner

2.0.1 • Public • Published

Gulp plugin for karma

CIImg GrkeepImg CovImg MntImg

NPMImg

What this?

This is a gulp plugin for karma

How can I use it?

Generally, you can use it by normal approach. i.e. like this:

gulpfile.js

((require) => {
  "use strict";
  const g = require('gulp');
  const karma = require('gulp-karma-runner');
  g.task(
    'test', () => g.src(['src/**/*.js', 'test/**/*.js'], {read: false})
      .pipe(karma.server({
        singleRun: true,
        autoWatch: false,
        frameworks: ['mocha', 'chai'],
        browsers: ['ChromeHeadless'],
        plugins: [
          'karma-mocha', 'karma-chai',
          'karma-chrome-launcher'
        ]
      }));
  );
}(require));

Note that the options are equivalent to Karma configucation options

Deal with gulp.watch

Considering browser ready event, dealing with gulp.watch might be a little-bit tricky, because you might need to start runner task after ensuring the server is ready, while gulp task finishes when the server is closed.

To handle this issue, you can declare the server task as asynchronous task, and call the callback when karma.server.browsers_ready event is emitted. In particular, like this:

gulpfile.js

((req) => {
  const gulp = req('gulp');
  const karma = req('gulp-karma-runner');
  const config = {
    autoWatch: false,
    quiet: true,
    frameworks: ['mocha', 'chai'],
    browsers: ['ChromeHeadless'],
    plugins: [
      'karma-mocha', 'karma-chai',
      'karma-chrome-launcher'
    ]
  };
  gulp.task(
    'server',
    done => gulp.src(['src/**/*.js', 'tests/**/*.js'], { read:false })
      .pipe(karma.server(config))
      .once('karma.server.browsers_ready', () => done());
  });
  gulp.task(
    'runner',
    () => gulp.src(['src/**/*.js', 'tests/**/*.js'], { read:false })
      .pipe(karma.runner(config));
  );
  gulp.task(
    'default',
    gulp.series(
      'server',
      () => gulp.watch(
        ['src/**/*.js', 'tests/**/*.js'],
        gulp.series('runner')
      ),
    )
  );
})(require);

Contribution

Before you create an issue/pull request,

  1. Read the code
  2. Read the code again
  3. Understand the code
  4. Understand the code again.

/gulp-karma-runner/

    Package Sidebar

    Install

    npm i gulp-karma-runner

    Weekly Downloads

    82

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    13.4 MB

    Total Files

    57

    Last publish

    Collaborators

    • hyamamoto