Skip to content

Commit f9de4ec

Browse files
committed
Cleanup #151
1 parent 3e55175 commit f9de4ec

11 files changed

Lines changed: 123 additions & 128 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text=auto
2+
*.js text eol=lf

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- '4'
54
- '6'
6-
- '7'
5+
- '4'

index.js

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
'use strict';
2-
32
const dargs = require('dargs');
43
const execa = require('execa');
54
const gutil = require('gulp-util');
65
const through = require('through2');
76

8-
module.exports = options => {
9-
const defaults = {colors: true, suppress: false};
10-
11-
options = Object.assign(defaults, options);
12-
13-
if (Object.prototype.toString.call(options.globals) === '[object Array]') {
14-
// typically wouldn't modify passed options, but mocha requires a comma-
15-
// separated list of names, http://mochajs.org/#globals-names, whereas dargs
16-
// will treat arrays differently.
17-
options.globals = options.globals.join(',');
18-
}
19-
20-
// exposing args for testing
21-
const args = dargs(options, {excludes: ['suppress'], ignoreFalse: true});
22-
const files = [];
23-
24-
function aggregate(file, encoding, done) {
25-
if (file.isNull()) {
26-
return done(null, file);
27-
}
28-
29-
if (file.isStream()) {
30-
return done(new gutil.PluginError('gulp-mocha', 'Streaming not supported'));
31-
}
32-
33-
files.push(file.path);
34-
35-
return done();
36-
}
37-
38-
function flush(done) {
39-
execa('mocha', files.concat(args))
40-
.then(result => {
41-
if (!options.suppress) {
42-
process.stdout.write(result.stdout);
43-
}
44-
45-
this.emit('result', result);
46-
done();
47-
})
48-
.catch(err => {
49-
this.emit('error', new gutil.PluginError('gulp-mocha', err));
50-
done();
51-
});
52-
}
53-
54-
return through.obj(aggregate, flush);
7+
module.exports = opts => {
8+
opts = Object.assign({
9+
colors: true,
10+
suppress: false
11+
}, opts);
12+
13+
if (Array.isArray(opts.globals)) {
14+
// `globals` option should end up as a comma-separated list
15+
opts.globals = opts.globals.join(',');
16+
}
17+
18+
const args = dargs(opts, {
19+
excludes: ['suppress'],
20+
ignoreFalse: true
21+
});
22+
23+
const files = [];
24+
25+
function aggregate(file, encoding, done) {
26+
if (file.isNull()) {
27+
done(null, file);
28+
return;
29+
}
30+
31+
if (file.isStream()) {
32+
done(new gutil.PluginError('gulp-mocha', 'Streaming not supported'));
33+
return;
34+
}
35+
36+
files.push(file.path);
37+
38+
done();
39+
}
40+
41+
function flush(done) {
42+
execa('mocha', files.concat(args))
43+
.then(result => {
44+
if (!opts.suppress) {
45+
process.stdout.write(result.stdout);
46+
}
47+
48+
// For testing
49+
this.emit('_result', result);
50+
51+
done();
52+
})
53+
.catch(err => {
54+
this.emit('error', new gutil.PluginError('gulp-mocha', err));
55+
done();
56+
});
57+
}
58+
59+
return through.obj(aggregate, flush);
5560
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"envs": [
4747
"node",
4848
"mocha"
49-
],
50-
"space": true
49+
]
5150
}
5251
}

readme.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
---
1010

11-
<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>
11+
<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>
1212

1313
---
1414

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

2929
gulp.task('default', () =>
3030
gulp.src('test.js', {read: false})
31-
// gulp-mocha needs filepaths so you can't have any plugins before it
31+
// `gulp-mocha` needs filepaths so you can't have any plugins before it
3232
.pipe(mocha({reporter: 'nyan'}))
3333
);
3434
```
3535

36-
> 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.
37-
3836

3937
## API
4038

4139
### mocha([options])
4240

4341
#### options
4442

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

4946
##### ui
5047

@@ -123,10 +120,6 @@ gulp.task('default', () =>
123120
);
124121
```
125122

126-
### Babel
127-
128-
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/).
129-
130123

131124
## License
132125

test/fixtures/fixture-async.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/* global it */
21
'use strict';
3-
var assert = require('assert');
2+
const assert = require('assert');
43

5-
it('should fail after timeout', function (done) {
6-
setTimeout(function () {
7-
assert(false);
8-
}, 10);
4+
it('should fail after timeout', (done) => {
5+
setTimeout(() => {
6+
assert(false);
7+
}, 10);
98
});

test/fixtures/fixture-fail.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
var assert = require('assert');
2+
const assert = require('assert');
33

4-
it('should fail', function () {
5-
assert(false);
4+
it('should fail', () => {
5+
assert(false);
66
});

test/fixtures/fixture-pass.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
var assert = require('assert');
2+
const assert = require('assert');
33

4-
it('should pass', function () {
5-
assert(true);
4+
it('should pass', () => {
5+
assert(true);
66
});
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
var assert = require('assert');
2+
const assert = require('assert');
33

4-
it('throws after timeout', function (done) {
5-
setTimeout(function () {
6-
throw new Error('Exception in delayed function');
7-
}, 10);
4+
it('throws after timeout', () => {
5+
setTimeout(() => {
6+
throw new Error('Exception in delayed function');
7+
}, 10);
88
});

test/fixtures/fixture-throws.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
var assert = require('assert');
2+
const assert = require('assert');
33

4-
it('contains syntax errors', function () {
5-
assert false;
4+
it('contains syntax errors', () => {
5+
assert false;
66
});

0 commit comments

Comments
 (0)