Skip to content

Commit

Permalink
Update Mocha and require Node.js and Gulp 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 18, 2019
1 parent cde8dc2 commit 4924437
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
60 changes: 27 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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);
Expand Down
37 changes: 26 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -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"
]
}
}
28 changes: 8 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

**[Maintainer needed](https://github.com/sindresorhus/gulp-mocha/issues/128)**

---

<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>

---


## Install

Expand All @@ -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'}))
Expand All @@ -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`<br>
Expand All @@ -65,14 +58,14 @@ This option can also be used to utilize third-party reporters. For example, if y

##### reporterOptions

Type: `Object`<br>
Type: `object`<br>
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).

Expand Down Expand Up @@ -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.

Expand All @@ -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 => {
Expand All @@ -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)
14 changes: 1 addition & 13 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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(',');
}
Expand Down

0 comments on commit 4924437

Please sign in to comment.