From 4924437d9f3110843fa16d25302a0efd6f49bc76 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 18 Aug 2019 23:43:21 +0200 Subject: [PATCH] Update Mocha and require Node.js and Gulp 4 --- .travis.yml | 2 +- gulpfile.js | 2 +- index.js | 60 +++++++++++++++++++++++----------------------------- package.json | 37 ++++++++++++++++++++++---------- readme.md | 28 +++++++----------------- utils.js | 14 +----------- 6 files changed, 64 insertions(+), 79 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2ae9d62..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '12' - '10' - '8' - - '6' diff --git a/gulpfile.js b/gulpfile.js index 7519b0b..a8ca47a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,7 +2,7 @@ const gulp = require('gulp'); const mocha = require('.'); -gulp.task('default', () => +exports.default = () => ( gulp.src('test/fixtures/fixture-pass.js', {read: false}) .pipe(mocha()) ); diff --git a/index.js b/index.js index 2200a2a..8888643 100644 --- a/index.js +++ b/index.js @@ -4,38 +4,33 @@ const execa = require('execa'); const PluginError = require('plugin-error'); const supportsColor = require('supports-color'); const through = require('through2'); -// TODO: Use execa localDir option when available -const npmRunPath = require('npm-run-path'); const utils = require('./utils'); -const HUNDRED_MEGABYTES = 1000 * 1000 * 100; - // Mocha options that can be specified multiple times const MULTIPLE_OPTS = new Set([ 'require' ]); -module.exports = opts => { - opts = Object.assign({ +module.exports = options => { + options = { colors: Boolean(supportsColor.stdout), - suppress: false - }, opts); - - for (const key of Object.keys(opts)) { - const val = opts[key]; + suppress: false, + ...options + }; - if (Array.isArray(val)) { + for (const [key, value] of Object.entries(options)) { + if (Array.isArray(value)) { if (!MULTIPLE_OPTS.has(key)) { // Convert arrays into comma separated lists - opts[key] = val.join(','); + options[key] = value.join(','); } - } else if (typeof val === 'object') { + } else if (typeof value === 'object') { // Convert an object into comma separated list - opts[key] = utils.convertObjectToList(val); + options[key] = utils.convertObjectToList(value); } } - const args = dargs(opts, { + const args = dargs(options, { excludes: ['suppress'], ignoreFalse: true }); @@ -54,26 +49,25 @@ module.exports = opts => { } function flush(done) { - const env = npmRunPath.env({cwd: __dirname}); - const proc = execa('mocha', files.concat(args), { - env, - maxBuffer: HUNDRED_MEGABYTES - }); + (async () => { + const subprocess = execa('mocha', files.concat(args), { + localDir: __dirname + }); + + if (!options.suppress) { + subprocess.stdout.pipe(subprocess.stdout); + subprocess.stderr.pipe(subprocess.stderr); + } - proc - .then(result => { + try { + const result = await subprocess; this.emit('_result', result); - done(); - }) - .catch(err => { - this.emit('error', new PluginError('gulp-mocha', err.code > 0 ? 'There were test failures' : err)); - done(); - }); + } catch (error) { + this.emit('error', new PluginError('gulp-mocha', error.exitCode > 0 ? 'There were test failures' : error)); + } - if (!opts.suppress) { - proc.stdout.pipe(process.stdout); - proc.stderr.pipe(process.stderr); - } + done(); + })(); } return through.obj(aggregate, flush); diff --git a/package.json b/package.json index 0b287ed..6c76e94 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { "test": "xo && ava" @@ -34,19 +34,34 @@ "tap" ], "dependencies": { - "dargs": "^5.1.0", - "execa": "^0.10.0", - "mocha": "^5.2.0", - "npm-run-path": "^2.0.2", + "dargs": "^7.0.0", + "execa": "^2.0.4", + "mocha": "^6.2.0", "plugin-error": "^1.0.1", - "supports-color": "^5.4.0", - "through2": "^2.0.3" + "supports-color": "^7.0.0", + "through2": "^3.0.1" }, "devDependencies": { - "ava": "*", - "gulp": "^3.9.1", - "p-event": "^1.0.0", + "ava": "^2.3.0", + "gulp": "^4.0.2", + "p-event": "^4.1.0", "vinyl": "^2.1.0", - "xo": "*" + "xo": "^0.24.0" + }, + "peerDependencies": { + "gulp": ">=4" + }, + "xo": { + "ignores": [ + "test/fixtures" + ], + "rules": { + "ava/no-ignored-test-files": "off" + } + }, + "ava": { + "files": [ + "test/test.js" + ] } } diff --git a/readme.md b/readme.md index 2ed822a..cbe86fe 100644 --- a/readme.md +++ b/readme.md @@ -6,12 +6,6 @@ **[Maintainer needed](https://github.com/sindresorhus/gulp-mocha/issues/128)** ---- - -

🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos. You might also like his React course.

- ---- - ## Install @@ -26,7 +20,7 @@ $ npm install --save-dev gulp-mocha const gulp = require('gulp'); const mocha = require('gulp-mocha'); -gulp.task('default', () => +exports.default = () => ( gulp.src('test.js', {read: false}) // `gulp-mocha` needs filepaths so you can't have any plugins before it .pipe(mocha({reporter: 'nyan'})) @@ -36,15 +30,14 @@ gulp.task('default', () => ## API -### mocha([options]) +### mocha(options?) #### options -Type: `Object` +Type: `object` 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. Arrays and key/value objects are correctly converted to the comma separated list format Mocha expects. Listed below are some of the more commonly used options: - ##### ui Type: `string`
@@ -65,14 +58,14 @@ This option can also be used to utilize third-party reporters. For example, if y ##### reporterOptions -Type: `Object`
+Type: `object`
Example: `{reportFilename: 'index.html'}` Reporter specific options. ##### globals -Type: `Array` +Type: `string[]` List of accepted global variable names, example `['YUI']`. Accepts wildcards to match multiple global variables, e.g. `['gulp*']` or even `['*']`. See [Mocha globals option](http://mochajs.org/#globals-option). @@ -105,7 +98,7 @@ Only run tests matching the given pattern which is internally compiled to a RegE ##### require -Type: `Array` +Type: `string[]` Require custom modules before tests are run. @@ -124,7 +117,7 @@ Specify a compiler. 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', () => +exports.default = () => ( gulp.src('test.js') .pipe(mocha()) .once('error', err => { @@ -140,14 +133,9 @@ gulp.task('default', () => Or you might just need to pass the `exit` option: ```js -gulp.task('test', () => +exports.test = () => ( gulp.src(['test/**/*.js'], {read: false}) .pipe(mocha({reporter: 'list', exit: true})) .on('error', console.error) ); ``` - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/utils.js b/utils.js index ce40c59..693f93d 100644 --- a/utils.js +++ b/utils.js @@ -1,19 +1,7 @@ 'use strict'; -// TODO: Use `Object.entries` when targeting Node.js 8 -function objectEntries(object) { - const entries = []; - - for (const key of Object.keys(object)) { - const value = object[key]; - entries.push([key, value]); - } - - return entries; -} - function convertObjectToList(object) { - return objectEntries(object) + return Object.entries(object) .reduce((result, current) => result.concat(`${current[0]}=${current[1]}`), []) .join(','); }