Skip to content

Commit

Permalink
Spawn Mocha instead of using its API (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored and sindresorhus committed Feb 20, 2017
1 parent 212def2 commit 3e55175
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 277 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: node_js
node_js:
- '6'
- '4'
- '0.12'
- '0.10'
- '6'
- '7'
121 changes: 52 additions & 69 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,55 @@
'use strict';
var domain = require('domain'); // eslint-disable-line no-restricted-modules
var gutil = require('gulp-util');
var through = require('through');
var Mocha = require('mocha');
var plur = require('plur');
var reqCwd = require('req-cwd');

module.exports = function (opts) {
opts = opts || {};

var mocha = new Mocha(opts);
var cache = {};

for (var key in require.cache) { // eslint-disable-line guard-for-in
cache[key] = true;
}

function clearCache() {
for (var key in require.cache) {
if (!cache[key] && !/\.node$/.test(key)) {
delete require.cache[key];
}
}
}

if (Array.isArray(opts.require) && opts.require.length) {
opts.require.forEach(function (x) {
reqCwd(x);
});
}

return through(function (file) {
mocha.addFile(file.path);
this.queue(file);
}, function () {
var self = this;
var d = domain.create();
var runner;

function handleException(err) {
if (runner) {
runner.uncaught(err);
} else {
clearCache();
self.emit('error', new gutil.PluginError('gulp-mocha', err, {
stack: err.stack,
showStack: true
}));
}
}

d.on('error', handleException);
d.run(function () {
try {
runner = mocha.run(function (errCount) {
clearCache();

if (errCount > 0) {
self.emit('error', new gutil.PluginError('gulp-mocha', errCount + ' ' + plur('test', errCount) + ' failed.', {
showStack: false
}));
}

self.emit('end');
});
} catch (err) {
handleException(err);
}
});
});
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);
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && mocha"
Expand All @@ -33,12 +33,11 @@
"tap"
],
"dependencies": {
"dargs": "^5.1.0",
"execa": "^0.6.0",
"gulp-util": "^3.0.0",
"mocha": "^3.0.0",
"plur": "^2.1.0",
"req-cwd": "^1.0.1",
"temp": "^0.8.3",
"through": "^2.3.4"
"through2": "^2.0.3"
},
"devDependencies": {
"xo": "*"
Expand All @@ -47,6 +46,7 @@
"envs": [
"node",
"mocha"
]
],
"space": true
}
}
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ npm install --save-dev gulp-mocha
const gulp = require('gulp');
const mocha = require('gulp-mocha');

gulp.task('default', () =>
gulp.task('default', () =>
gulp.src('test.js', {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha({reporter: 'nyan'}))
Expand All @@ -42,6 +42,10 @@ 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:

##### ui

Type: `string`<br>
Expand Down Expand Up @@ -80,12 +84,12 @@ Default: `false`

Bail on the first test failure.

##### ignoreLeaks
##### checkLeaks

Type: `boolean`<br>
Default: `false`

Ignore global leaks.
Check for global variable leaks.

##### grep

Expand All @@ -107,7 +111,7 @@ Require custom modules before tests are run.
If your test suite is not exiting it might be because you still have a lingering callback, most often caused by an open database connection. You should close this connection or do the following:

```js
gulp.task('default', () =>
gulp.task('default', () =>
gulp.src('test.js')
.pipe(mocha())
.once('error', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/fixture-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var assert = require('assert');

it('should fail after timeout', function (done) {
setTimeout(function () {
assert(false);
}, 10);
setTimeout(function () {
assert(false);
}, 10);
});
2 changes: 1 addition & 1 deletion test/fixtures/fixture-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
var assert = require('assert');

it('should fail', function () {
assert(false);
assert(false);
});
2 changes: 1 addition & 1 deletion test/fixtures/fixture-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
var assert = require('assert');

it('should pass', function () {
assert(true);
assert(true);
});
6 changes: 3 additions & 3 deletions test/fixtures/fixture-throws-uncaught.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var assert = require('assert');

it('throws after timeout', function (done) {
setTimeout(function () {
throw new Error('Exception in delayed function');
}, 10);
setTimeout(function () {
throw new Error('Exception in delayed function');
}, 10);
});
2 changes: 1 addition & 1 deletion test/fixtures/fixture-throws.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
var assert = require('assert');

it('contains syntax errors', function () {
assert false;
assert false;
});
49 changes: 0 additions & 49 deletions test/require-test.js

This file was deleted.

Loading

0 comments on commit 3e55175

Please sign in to comment.