-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spawn Mocha instead of using its API (#151)
- Loading branch information
1 parent
212def2
commit 3e55175
Showing
11 changed files
with
139 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
Oops, something went wrong.