-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be documented. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was only meant to be used internally for tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then it should be prefixed with an underscore: |
||
done(); | ||
}) | ||
.catch(err => { | ||
this.emit('error', new gutil.PluginError('gulp-mocha', err)); | ||
done(); | ||
}); | ||
} | ||
|
||
return through.obj(aggregate, flush); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'})) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -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 | ||
|
||
|
@@ -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', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
var assert = require('assert'); | ||
|
||
it('should fail', function () { | ||
assert(false); | ||
assert(false); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
var assert = require('assert'); | ||
|
||
it('should pass', function () { | ||
assert(true); | ||
assert(true); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
var assert = require('assert'); | ||
|
||
it('contains syntax errors', function () { | ||
assert false; | ||
assert false; | ||
}); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
Array.isArray()
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, my fault there.