diff --git a/.gitattributes b/.gitattributes index 176a458..391f0a4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ * text=auto +*.js text eol=lf diff --git a/.travis.yml b/.travis.yml index 96ce5e6..b18bae5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ sudo: false language: node_js node_js: - - '4' - '6' - - '7' + - '4' diff --git a/index.js b/index.js index 621fc93..dfae8a4 100644 --- a/index.js +++ b/index.js @@ -1,55 +1,60 @@ 'use strict'; - const dargs = require('dargs'); const execa = require('execa'); const gutil = require('gulp-util'); const through = require('through2'); -module.exports = options => { - const defaults = {colors: true, suppress: false}; - - options = Object.assign(defaults, options); - - if (Object.prototype.toString.call(options.globals) === '[object Array]') { - // typically wouldn't modify passed options, but mocha requires a comma- - // separated list of names, http://mochajs.org/#globals-names, whereas dargs - // will treat arrays differently. - options.globals = options.globals.join(','); - } - - // exposing args for testing - const args = dargs(options, {excludes: ['suppress'], ignoreFalse: true}); - const files = []; - - function aggregate(file, encoding, done) { - if (file.isNull()) { - return done(null, file); - } - - if (file.isStream()) { - return done(new gutil.PluginError('gulp-mocha', 'Streaming not supported')); - } - - files.push(file.path); - - return done(); - } - - function flush(done) { - execa('mocha', files.concat(args)) - .then(result => { - if (!options.suppress) { - process.stdout.write(result.stdout); - } - - this.emit('result', result); - done(); - }) - .catch(err => { - this.emit('error', new gutil.PluginError('gulp-mocha', err)); - done(); - }); - } - - return through.obj(aggregate, flush); +module.exports = opts => { + opts = Object.assign({ + colors: true, + suppress: false + }, opts); + + if (Array.isArray(opts.globals)) { + // `globals` option should end up as a comma-separated list + opts.globals = opts.globals.join(','); + } + + const args = dargs(opts, { + excludes: ['suppress'], + ignoreFalse: true + }); + + const files = []; + + function aggregate(file, encoding, done) { + if (file.isNull()) { + done(null, file); + return; + } + + if (file.isStream()) { + done(new gutil.PluginError('gulp-mocha', 'Streaming not supported')); + return; + } + + files.push(file.path); + + done(); + } + + function flush(done) { + execa('mocha', files.concat(args)) + .then(result => { + if (!opts.suppress) { + process.stdout.write(result.stdout); + } + + // For testing + this.emit('_result', result); + + done(); + }) + .catch(err => { + this.emit('error', new gutil.PluginError('gulp-mocha', err)); + done(); + }); + } + + return through.obj(aggregate, flush); }; diff --git a/package.json b/package.json index 74886d7..aabaa53 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "envs": [ "node", "mocha" - ], - "space": true + ] } } diff --git a/readme.md b/readme.md index 1fd231f..10ddbf5 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ --- -

🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos.

+

🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos. You might also like his React course.

--- @@ -28,13 +28,11 @@ const mocha = require('gulp-mocha'); gulp.task('default', () => gulp.src('test.js', {read: false}) - // gulp-mocha needs filepaths so you can't have any plugins before it + // `gulp-mocha` needs filepaths so you can't have any plugins before it .pipe(mocha({reporter: 'nyan'})) ); ``` -> If you are writing a watch task to run your tests as you modify your `.js` files, be aware that you might run into issues. This plugin runs your Mocha tests within the same process as your watch task and state isn't reset between runs. If your tests eventually fail within the watch task but pass when run in a standalone task or with `mocha test`, then you need to use the [`gulp-spawn-mocha`](https://github.com/KenPowers/gulp-spawn-mocha) plugin. - ## API @@ -42,9 +40,8 @@ gulp.task('default', () => #### options -gulp-mocha will pass any options defined directly to the `mocha` binary. That -means you have every [command line option](http://mochajs.org/#usage) available -by default. Listed below are some of the more commonly used options: +Options are passed directly to the `mocha` binary, so you can use any its [command-line options](http://mochajs.org/#usage) in a camelCased form. Listed below are some of the more commonly used options: + ##### ui @@ -123,10 +120,6 @@ gulp.task('default', () => ); ``` -### Babel - -Add `require('babel-core/register');` to the top of your `gulpfile.js`. Make sure to read the [Babel docs](https://babeljs.io/docs/usage/require/). - ## License diff --git a/test/fixtures/fixture-async.js b/test/fixtures/fixture-async.js index 49eb53c..783b67f 100644 --- a/test/fixtures/fixture-async.js +++ b/test/fixtures/fixture-async.js @@ -1,9 +1,8 @@ -/* global it */ 'use strict'; -var assert = require('assert'); +const assert = require('assert'); -it('should fail after timeout', function (done) { - setTimeout(function () { - assert(false); - }, 10); +it('should fail after timeout', (done) => { + setTimeout(() => { + assert(false); + }, 10); }); diff --git a/test/fixtures/fixture-fail.js b/test/fixtures/fixture-fail.js index 1a19cfe..08d9aac 100644 --- a/test/fixtures/fixture-fail.js +++ b/test/fixtures/fixture-fail.js @@ -1,6 +1,6 @@ 'use strict'; -var assert = require('assert'); +const assert = require('assert'); -it('should fail', function () { - assert(false); +it('should fail', () => { + assert(false); }); diff --git a/test/fixtures/fixture-pass.js b/test/fixtures/fixture-pass.js index 2f5472e..d9bbd07 100644 --- a/test/fixtures/fixture-pass.js +++ b/test/fixtures/fixture-pass.js @@ -1,6 +1,6 @@ 'use strict'; -var assert = require('assert'); +const assert = require('assert'); -it('should pass', function () { - assert(true); +it('should pass', () => { + assert(true); }); diff --git a/test/fixtures/fixture-throws-uncaught.js b/test/fixtures/fixture-throws-uncaught.js index 2231295..79439f9 100644 --- a/test/fixtures/fixture-throws-uncaught.js +++ b/test/fixtures/fixture-throws-uncaught.js @@ -1,8 +1,8 @@ 'use strict'; -var assert = require('assert'); +const assert = require('assert'); -it('throws after timeout', function (done) { - setTimeout(function () { - throw new Error('Exception in delayed function'); - }, 10); +it('throws after timeout', () => { + setTimeout(() => { + throw new Error('Exception in delayed function'); + }, 10); }); diff --git a/test/fixtures/fixture-throws.js b/test/fixtures/fixture-throws.js index ae70231..841f8a1 100644 --- a/test/fixtures/fixture-throws.js +++ b/test/fixtures/fixture-throws.js @@ -1,6 +1,6 @@ 'use strict'; -var assert = require('assert'); +const assert = require('assert'); -it('contains syntax errors', function () { - assert false; +it('contains syntax errors', () => { + assert false; }); diff --git a/test/test.js b/test/test.js index 98c50f0..35d09ec 100644 --- a/test/test.js +++ b/test/test.js @@ -1,65 +1,64 @@ 'use strict'; - const assert = require('assert'); const fs = require('fs'); const path = require('path'); const gutil = require('gulp-util'); -const mocha = require('../'); +const mocha = require('..'); function fixture(name) { - let fileName = path.join(__dirname, 'fixtures', name); + const fileName = path.join(__dirname, 'fixtures', name); - return new gutil.File({ - path: fileName, - contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null - }); + return new gutil.File({ + path: fileName, + contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null + }); } describe('mocha()', () => { - it('should run unit test and pass', done => { - let stream = mocha({suppress: true}); + it('should run unit test and pass', done => { + const stream = mocha({suppress: true}); - stream.once('result', result => { - assert(/1 passing/.test(result.stdout)); - done(); - }); - stream.write(fixture('fixture-pass.js')); - stream.end(); - }); + stream.once('_result', result => { + assert(/1 passing/.test(result.stdout)); + done(); + }); + stream.write(fixture('fixture-pass.js')); + stream.end(); + }); - it('should run unit test and fail', done => { - let stream = mocha({suppress: true}); + it('should run unit test and fail', done => { + const stream = mocha({suppress: true}); - stream.once('error', function (err) { - assert(/1 failing/.test(err.stdout)); - done(); - }); - stream.write(fixture('fixture-fail.js')); - stream.end(); - }); + stream.once('error', err => { + assert(/1 failing/.test(err.stdout)); + done(); + }); + stream.write(fixture('fixture-fail.js')); + stream.end(); + }); - it('should pass async AssertionError to mocha', function (done) { - let stream = mocha({suppress: true}); + it('should pass async AssertionError to mocha', done => { + const stream = mocha({suppress: true}); - stream.once('error', function (err) { - let throws = /throws after timeout/.test(err.stdout); - let uncaught = /Uncaught AssertionError: false == true/.test(err.stdout); + stream.once('error', err => { + const throws = /throws after timeout/.test(err.stdout); + const uncaught = /Uncaught AssertionError: false == true/.test(err.stdout); - assert(throws || uncaught); - done(); - }); - stream.write(fixture('fixture-async.js')); - stream.end(); - }); + assert(throws || uncaught); + done(); + }); + stream.write(fixture('fixture-async.js')); + stream.end(); + }); - it('should not suppress output', done => { - let stream = mocha(); + it('should not suppress output', done => { + const stream = mocha(); - stream.once('result', result => { - assert(/should pass/.test(result.stdout)); - done(); - }); - stream.write(fixture('fixture-pass.js')); - stream.end(); - }); + stream.once('_result', result => { + assert(/should pass/.test(result.stdout)); + done(); + }); + stream.write(fixture('fixture-pass.js')); + stream.end(); + }); });