Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn Mocha instead of using its API #151

Merged
merged 3 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Array.isArray()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not compatible with with node < 6. http://node.green/#Array-isArray-support

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. You're looking at proxy support. Array.isArray() has been available since at least Node.js 0.10.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, my fault there.

// 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was only meant to be used internally for tests

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it should be prefixed with an underscore: _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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be explicit about it users using a camelCased version of those command line options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dargs doesn't require that you use only camelCased args, it treats the arguments the same if they're in sausage case (the format of the mocha args) as well.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the point. Users don't want to use quotes in the keys. And they wouldn't even know camelCase is supported.

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