gulp-mocha
Advanced tools
Comparing version 3.0.1 to 4.0.0
106
index.js
'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'); | ||
const dargs = require('dargs'); | ||
const execa = require('execa'); | ||
const gutil = require('gulp-util'); | ||
const through = require('through2'); | ||
module.exports = function (opts) { | ||
opts = opts || {}; | ||
module.exports = opts => { | ||
opts = Object.assign({ | ||
colors: true, | ||
suppress: false | ||
}, opts); | ||
var mocha = new Mocha(opts); | ||
var cache = {}; | ||
for (var key in require.cache) { // eslint-disable-line guard-for-in | ||
cache[key] = true; | ||
if (Array.isArray(opts.globals)) { | ||
// `globals` option should end up as a comma-separated list | ||
opts.globals = opts.globals.join(','); | ||
} | ||
function clearCache() { | ||
for (var key in require.cache) { | ||
if (!cache[key] && !/\.node$/.test(key)) { | ||
delete require.cache[key]; | ||
} | ||
const args = dargs(opts, { | ||
excludes: ['suppress'], | ||
ignoreFalse: true | ||
}); | ||
const files = []; | ||
function aggregate(file, encoding, done) { | ||
if (file.isNull()) { | ||
done(null, file); | ||
return; | ||
} | ||
} | ||
if (Array.isArray(opts.require) && opts.require.length) { | ||
opts.require.forEach(function (x) { | ||
reqCwd(x); | ||
}); | ||
if (file.isStream()) { | ||
done(new gutil.PluginError('gulp-mocha', 'Streaming not supported')); | ||
return; | ||
} | ||
files.push(file.path); | ||
done(); | ||
} | ||
return through(function (file) { | ||
mocha.addFile(file.path); | ||
this.queue(file); | ||
}, function () { | ||
var self = this; | ||
var d = domain.create(); | ||
var runner; | ||
function flush(done) { | ||
execa('mocha', files.concat(args)) | ||
.then(result => { | ||
if (!opts.suppress) { | ||
process.stdout.write(result.stdout); | ||
} | ||
function handleException(err) { | ||
if (runner) { | ||
runner.uncaught(err); | ||
} else { | ||
clearCache(); | ||
self.emit('error', new gutil.PluginError('gulp-mocha', err, { | ||
stack: err.stack, | ||
showStack: true | ||
})); | ||
} | ||
} | ||
// For testing | ||
this.emit('_result', result); | ||
d.on('error', handleException); | ||
d.run(function () { | ||
try { | ||
runner = mocha.run(function (errCount) { | ||
clearCache(); | ||
done(); | ||
}) | ||
.catch(err => { | ||
this.emit('error', new gutil.PluginError('gulp-mocha', err)); | ||
done(); | ||
}); | ||
} | ||
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); | ||
} | ||
}); | ||
}); | ||
return through.obj(aggregate, flush); | ||
}; |
{ | ||
"name": "gulp-mocha", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Run Mocha tests", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -37,8 +37,7 @@ "scripts": { | ||
"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" | ||
}, | ||
@@ -45,0 +44,0 @@ "devDependencies": { |
@@ -11,3 +11,3 @@ # gulp-mocha [![Build Status](https://travis-ci.org/sindresorhus/gulp-mocha.svg?branch=master)](https://travis-ci.org/sindresorhus/gulp-mocha) | ||
<p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</p> | ||
<p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React course</a>.</p> | ||
@@ -30,5 +30,5 @@ --- | ||
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 | ||
// `gulp-mocha` needs filepaths so you can't have any plugins before it | ||
.pipe(mocha({reporter: 'nyan'})) | ||
@@ -38,5 +38,3 @@ ); | ||
> 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 | ||
@@ -48,2 +46,5 @@ | ||
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 | ||
@@ -87,3 +88,3 @@ | ||
##### ignoreLeaks | ||
##### checkLeaks | ||
@@ -93,3 +94,3 @@ Type: `boolean`<br> | ||
Ignore global leaks. | ||
Check for global variable leaks. | ||
@@ -116,3 +117,3 @@ ##### grep | ||
```js | ||
gulp.task('default', () => | ||
gulp.task('default', () => | ||
gulp.src('test.js') | ||
@@ -129,9 +130,5 @@ .pipe(mocha()) | ||
### 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 | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
5
5932
48
127
+ Addeddargs@^5.1.0
+ Addedexeca@^0.6.0
+ Addedthrough2@^2.0.3
+ Addedcross-spawn@5.1.0(transitive)
+ Addeddargs@5.1.0(transitive)
+ Addedexeca@0.6.3(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedyallist@2.1.2(transitive)
- Removedplur@^2.1.0
- Removedreq-cwd@^1.0.1
- Removedtemp@^0.8.3
- Removedthrough@^2.3.4
- Removedglob@7.2.3(transitive)
- Removedirregular-plurals@1.4.0(transitive)
- Removedplur@2.1.2(transitive)
- Removedreq-cwd@1.0.1(transitive)
- Removedreq-from@1.0.1(transitive)
- Removedresolve-from@2.0.0(transitive)
- Removedrimraf@2.6.3(transitive)
- Removedtemp@0.8.4(transitive)
- Removedthrough@2.3.8(transitive)