Skip to content

Commit

Permalink
Move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 3, 2023
1 parent e7211df commit 93f854b
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Expand Up @@ -14,7 +14,7 @@ jobs:
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
14 changes: 7 additions & 7 deletions gulpfile.js
@@ -1,8 +1,8 @@
'use strict';
const gulp = require('gulp');
const mocha = require('./index.js');
import gulp from 'gulp';
import mocha from './index.js';

export default function main() {
return gulp.src('test/fixtures/fixture-pass.js', {read: false})
.pipe(mocha());
}

exports.default = () => (
gulp.src('test/fixtures/fixture-pass.js', {read: false})
.pipe(mocha())
);
77 changes: 39 additions & 38 deletions index.js
@@ -1,58 +1,57 @@
'use strict';
const dargs = require('dargs');
const execa = require('execa');
const PluginError = require('plugin-error');
const supportsColor = require('supports-color');
const through = require('through2');
const utils = require('./utils.js');
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import path from 'node:path';
import dargs from 'dargs';
import {execa} from 'execa';
import supportsColor from 'supports-color';
import {gulpPlugin} from 'gulp-plugin-extras';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Mocha options that can be specified multiple times
const MULTIPLE_OPTS = new Set([
'require'
const MULTIPLE_OPTIONS = new Set([
'require',
]);

module.exports = options => {
function convertObjectToList(object) {
return Object.entries(object)
.map(([key, value]) => `${key}=${value}`)
.join(',');
}

export default function gulpMocha(options) {
options = {
colors: Boolean(supportsColor.stdout),
suppress: false,
...options
...options,
};

for (const [key, value] of Object.entries(options)) {
if (Array.isArray(value)) {
if (!MULTIPLE_OPTS.has(key)) {
if (!MULTIPLE_OPTIONS.has(key)) {
// Convert arrays into comma separated lists
options[key] = value.join(',');
}
} else if (typeof value === 'object') {
// Convert an object into comma separated list
options[key] = utils.convertObjectToList(value);
options[key] = convertObjectToList(value);
}
}

const args = dargs(options, {
const arguments_ = dargs(options, {
excludes: ['suppress'],
ignoreFalse: true
ignoreFalse: true,
});

const files = [];

function aggregate(file, encoding, done) {
if (file.isStream()) {
done(new PluginError('gulp-mocha', 'Streaming not supported'));
return;
}

return gulpPlugin('gulp-mocha', file => {
files.push(file.path);

done();
}

function flush(done) {
(async () => {
const subprocess = execa('mocha', files.concat(args), {
}, {
async * onFinish(stream) { // eslint-disable-line require-yield
const subprocess = execa('mocha', [...files, ...arguments_], {
localDir: __dirname,
preferLocal: true
preferLocal: true,
});

if (!options.suppress) {
Expand All @@ -62,14 +61,16 @@ module.exports = options => {

try {
const result = await subprocess;
this.emit('_result', result);
stream.emit('_result', result);
} catch (error) {
this.emit('error', new PluginError('gulp-mocha', error.exitCode > 0 ? 'There were test failures' : error));
}

done();
})();
}
if (error.exitCode > 0) {
const error = new Error('There were test failures');
error.isPresentable = true;
throw error;
}

return through.obj(aggregate, flush);
};
throw error;
}
},
});
}
19 changes: 9 additions & 10 deletions package.json
Expand Up @@ -10,6 +10,7 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=18"
Expand All @@ -18,8 +19,7 @@
"test": "xo && ava"
},
"files": [
"index.js",
"utils.js"
"index.js"
],
"keywords": [
"gulpplugin",
Expand All @@ -36,19 +36,18 @@
"tap"
],
"dependencies": {
"dargs": "^7.0.0",
"execa": "^5.0.0",
"dargs": "^8.1.0",
"execa": "^8.0.1",
"gulp-plugin-extras": "^0.3.0",
"mocha": "^10.2.0",
"plugin-error": "^1.0.1",
"supports-color": "^8.1.1",
"through2": "^4.0.2"
"supports-color": "^9.4.0"
},
"devDependencies": {
"ava": "^2.3.0",
"ava": "^5.3.1",
"gulp": "^4.0.2",
"p-event": "^4.2.0",
"p-event": "^6.0.0",
"vinyl": "^3.0.0",
"xo": "^0.37.1"
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand Down
44 changes: 20 additions & 24 deletions readme.md
Expand Up @@ -4,28 +4,25 @@
*Keep in mind that this is just a thin wrapper around Mocha and your issue is most likely with Mocha.*


## Install

```sh
npm install --save-dev gulp-mocha
```
$ npm install --save-dev gulp-mocha
```


## Usage

```js
const gulp = require('gulp');
const mocha = require('gulp-mocha');
import gulp from 'gulp';
import mocha from 'gulp-mocha';

exports.default = () => (
export default () => (
gulp.src('test.js', {read: false})
// `gulp-mocha` needs filepaths so you can't have any plugins before it
// `gulp-mocha` needs file paths so you cannot have any plugins before it.
.pipe(mocha({reporter: 'nyan'}))
);
```


## API

### mocha(options?)
Expand All @@ -38,25 +35,25 @@ Options are passed directly to the `mocha` binary, so you can use any its [comma

##### ui

Type: `string`<br>
Default: `bdd`<br>
Values: `bdd` `tdd` `qunit` `exports`
Type: `string`\
Default: `'bdd'`\
Values: `'bdd' | 'tdd' | 'qunit' | 'exports'`

Interface to use.
The interface to use.

##### reporter

Type: `string`<br>
Default: `spec`
Type: `string`\
Default: `spec`\
Values: [Reporters](https://github.com/mochajs/mocha/tree/master/lib/reporters)

Reporter that will be used.
The reporter that will be used.

This option can also be used to utilize third-party reporters. For example, if you `npm install mocha-lcov-reporter` you can then do use `mocha-lcov-reporter` as value.

##### reporterOptions

Type: `object`<br>
Type: `object`\
Example: `{reportFilename: 'index.html'}`

Reporter specific options.
Expand All @@ -69,21 +66,21 @@ List of accepted global variable names, example `['YUI']`. Accepts wildcards to

##### timeout

Type: `number`<br>
Type: `number`\
Default: `2000`

Test-case timeout in milliseconds.

##### bail

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Bail on the first test failure.

##### checkLeaks

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Check for global variable leaks.
Expand All @@ -102,20 +99,19 @@ Require custom modules before tests are run.

##### compilers

Type: `string`<br>
Type: `string`\
Example: `js:babel-core/register`

Specify a compiler.


## FAQ

### Test suite not exiting

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
exports.default = () => (
export default () => (
gulp.src('test.js')
.pipe(mocha())
.once('error', err => {
Expand All @@ -131,7 +127,7 @@ exports.default = () => (
Or you might just need to pass the `exit` option:

```js
exports.test = () => (
export const test = () => (
gulp.src(['test/**/*.js'], {read: false})
.pipe(mocha({reporter: 'list', exit: true}))
.on('error', console.error)
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fixture-async.js
@@ -1,5 +1,4 @@
'use strict';
const assert = require('assert');
import assert from 'node:assert';

it('should fail after timeout', done => {
setTimeout(() => {
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fixture-fail.js
@@ -1,5 +1,4 @@
'use strict';
const assert = require('assert');
import assert from 'node:assert';

it('should fail', () => {
assert(false);
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fixture-pass.js
@@ -1,5 +1,4 @@
'use strict';
const assert = require('assert');
import assert from 'node:assert';

it('should pass', () => {
assert(true);
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fixture-throws-uncaught.js
@@ -1,5 +1,4 @@
'use strict';
const assert = require('assert');
import assert from 'node:assert';

it('throws after timeout', () => {
setTimeout(() => {
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fixture-throws.js
@@ -1,5 +1,4 @@
'use strict';
const assert = require('assert');
import assert from 'node:assert';

it('contains syntax errors', () => {
assert false;
Expand Down

0 comments on commit 93f854b

Please sign in to comment.