Skip to content

Commit

Permalink
Cleanup #151
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 20, 2017
1 parent 3e55175 commit f9de4ec
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 128 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '7'
- '4'
101 changes: 53 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
'use strict';

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);
module.exports = opts => {
opts = Object.assign({
colors: true,
suppress: false
}, opts);

if (Array.isArray(opts.globals)) {
// `globals` option should end up as a comma-separated list
opts.globals = opts.globals.join(',');
}

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

const files = [];

function aggregate(file, encoding, done) {
if (file.isNull()) {
done(null, file);
return;
}

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

files.push(file.path);

done();
}

function flush(done) {
execa('mocha', files.concat(args))
.then(result => {
if (!opts.suppress) {
process.stdout.write(result.stdout);
}

// For testing
this.emit('_result', result);

done();
})
.catch(err => {
this.emit('error', new gutil.PluginError('gulp-mocha', err));
done();
});
}

return through.obj(aggregate, flush);
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"envs": [
"node",
"mocha"
],
"space": true
]
}
}
15 changes: 4 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

---

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

---

Expand All @@ -28,23 +28,20 @@ const mocha = require('gulp-mocha');

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'}))
);
```

> 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

### mocha([options])

#### options

gulp-mocha will pass any options defined directly to the `mocha` binary. That
means you have every [command line option](http://mochajs.org/#usage) available
by default. Listed below are some of the more commonly used options:
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

Expand Down Expand Up @@ -123,10 +120,6 @@ gulp.task('default', () =>
);
```

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

Expand Down
11 changes: 5 additions & 6 deletions test/fixtures/fixture-async.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* global it */
'use strict';
var assert = require('assert');
const assert = require('assert');

it('should fail after timeout', function (done) {
setTimeout(function () {
assert(false);
}, 10);
it('should fail after timeout', (done) => {
setTimeout(() => {
assert(false);
}, 10);
});
6 changes: 3 additions & 3 deletions test/fixtures/fixture-fail.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var assert = require('assert');
const assert = require('assert');

it('should fail', function () {
assert(false);
it('should fail', () => {
assert(false);
});
6 changes: 3 additions & 3 deletions test/fixtures/fixture-pass.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var assert = require('assert');
const assert = require('assert');

it('should pass', function () {
assert(true);
it('should pass', () => {
assert(true);
});
10 changes: 5 additions & 5 deletions test/fixtures/fixture-throws-uncaught.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
var assert = require('assert');
const assert = require('assert');

it('throws after timeout', function (done) {
setTimeout(function () {
throw new Error('Exception in delayed function');
}, 10);
it('throws after timeout', () => {
setTimeout(() => {
throw new Error('Exception in delayed function');
}, 10);
});
6 changes: 3 additions & 3 deletions test/fixtures/fixture-throws.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var assert = require('assert');
const assert = require('assert');

it('contains syntax errors', function () {
assert false;
it('contains syntax errors', () => {
assert false;
});
89 changes: 44 additions & 45 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
'use strict';

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const gutil = require('gulp-util');
const mocha = require('../');
const mocha = require('..');

function fixture(name) {
let fileName = path.join(__dirname, 'fixtures', name);
const fileName = path.join(__dirname, 'fixtures', name);

return new gutil.File({
path: fileName,
contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null
});
return new gutil.File({
path: fileName,
contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null
});
}

describe('mocha()', () => {
it('should run unit test and pass', done => {
let stream = mocha({suppress: true});
it('should run unit test and pass', done => {
const stream = mocha({suppress: true});

stream.once('result', result => {
assert(/1 passing/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});
stream.once('_result', result => {
assert(/1 passing/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});

it('should run unit test and fail', done => {
let stream = mocha({suppress: true});
it('should run unit test and fail', done => {
const stream = mocha({suppress: true});

stream.once('error', function (err) {
assert(/1 failing/.test(err.stdout));
done();
});
stream.write(fixture('fixture-fail.js'));
stream.end();
});
stream.once('error', err => {
assert(/1 failing/.test(err.stdout));
done();
});
stream.write(fixture('fixture-fail.js'));
stream.end();
});

it('should pass async AssertionError to mocha', function (done) {
let stream = mocha({suppress: true});
it('should pass async AssertionError to mocha', done => {
const stream = mocha({suppress: true});

stream.once('error', function (err) {
let throws = /throws after timeout/.test(err.stdout);
let uncaught = /Uncaught AssertionError: false == true/.test(err.stdout);
stream.once('error', err => {
const throws = /throws after timeout/.test(err.stdout);
const uncaught = /Uncaught AssertionError: false == true/.test(err.stdout);

assert(throws || uncaught);
done();
});
stream.write(fixture('fixture-async.js'));
stream.end();
});
assert(throws || uncaught);
done();
});
stream.write(fixture('fixture-async.js'));
stream.end();
});

it('should not suppress output', done => {
let stream = mocha();
it('should not suppress output', done => {
const stream = mocha();

stream.once('result', result => {
assert(/should pass/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});
stream.once('_result', result => {
assert(/should pass/.test(result.stdout));
done();
});
stream.write(fixture('fixture-pass.js'));
stream.end();
});
});

0 comments on commit f9de4ec

Please sign in to comment.