Skip to content

Commit f9de4ec

Browse files
committedFeb 20, 2017
Cleanup #151
1 parent 3e55175 commit f9de4ec

11 files changed

+123
-128
lines changed
 

‎.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text=auto
2+
*.js text eol=lf

‎.travis.yml

+1-2
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

+53-48
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

+1-2
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

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

+5-6
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

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

+3-3
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
});
+5-5
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

+3-3
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
});

‎test/test.js

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
'use strict';
2-
32
const assert = require('assert');
43
const fs = require('fs');
54
const path = require('path');
65
const gutil = require('gulp-util');
7-
const mocha = require('../');
6+
const mocha = require('..');
87

98
function fixture(name) {
10-
let fileName = path.join(__dirname, 'fixtures', name);
9+
const fileName = path.join(__dirname, 'fixtures', name);
1110

12-
return new gutil.File({
13-
path: fileName,
14-
contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null
15-
});
11+
return new gutil.File({
12+
path: fileName,
13+
contents: fs.existsSync(fileName) ? fs.readFileSync(fileName) : null
14+
});
1615
}
1716

1817
describe('mocha()', () => {
19-
it('should run unit test and pass', done => {
20-
let stream = mocha({suppress: true});
18+
it('should run unit test and pass', done => {
19+
const stream = mocha({suppress: true});
2120

22-
stream.once('result', result => {
23-
assert(/1 passing/.test(result.stdout));
24-
done();
25-
});
26-
stream.write(fixture('fixture-pass.js'));
27-
stream.end();
28-
});
21+
stream.once('_result', result => {
22+
assert(/1 passing/.test(result.stdout));
23+
done();
24+
});
25+
stream.write(fixture('fixture-pass.js'));
26+
stream.end();
27+
});
2928

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

33-
stream.once('error', function (err) {
34-
assert(/1 failing/.test(err.stdout));
35-
done();
36-
});
37-
stream.write(fixture('fixture-fail.js'));
38-
stream.end();
39-
});
32+
stream.once('error', err => {
33+
assert(/1 failing/.test(err.stdout));
34+
done();
35+
});
36+
stream.write(fixture('fixture-fail.js'));
37+
stream.end();
38+
});
4039

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

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

48-
assert(throws || uncaught);
49-
done();
50-
});
51-
stream.write(fixture('fixture-async.js'));
52-
stream.end();
53-
});
47+
assert(throws || uncaught);
48+
done();
49+
});
50+
stream.write(fixture('fixture-async.js'));
51+
stream.end();
52+
});
5453

55-
it('should not suppress output', done => {
56-
let stream = mocha();
54+
it('should not suppress output', done => {
55+
const stream = mocha();
5756

58-
stream.once('result', result => {
59-
assert(/should pass/.test(result.stdout));
60-
done();
61-
});
62-
stream.write(fixture('fixture-pass.js'));
63-
stream.end();
64-
});
57+
stream.once('_result', result => {
58+
assert(/should pass/.test(result.stdout));
59+
done();
60+
});
61+
stream.write(fixture('fixture-pass.js'));
62+
stream.end();
63+
});
6564
});

0 commit comments

Comments
 (0)
Please sign in to comment.