forked from sindresorhus/gulp-mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var domain = require('domain');
var gutil = require('gulp-util');
var through = require('through');
var Mocha = require('mocha');
var plur = require('plur');
var reqCwd = require('req-cwd');
module.exports = function (opts) {
opts = opts || {};
var mocha = new Mocha(opts);
var cache = {};
for (var key in require.cache) {
cache[key] = true;
}
function clearCache() {
for (var key in require.cache) {
if (!cache[key] && !/\.node$/.test(key)) {
delete require.cache[key];
}
}
}
if (Array.isArray(opts.require) && opts.require.length) {
opts.require.forEach(function (x) {
reqCwd(x);
});
}
return through(function (file) {
mocha.addFile(file.path);
this.queue(file);
}, function () {
var self = this;
var d = domain.create();
var runner;
function handleException(err) {
if (runner) {
runner.uncaught(err);
} else {
clearCache();
self.emit('error', new gutil.PluginError('gulp-mocha', err, {
stack: err.stack,
showStack: true
}));
}
}
d.on('error', handleException);
d.run(function () {
try {
runner = mocha.run(function (errCount) {
clearCache();
if (errCount > 0) {
self.emit('error', new gutil.PluginError('gulp-mocha', errCount + ' ' + plur('test', errCount) + ' failed.', {
showStack: false
}));
}
self.emit('end');
});
} catch (err) {
handleException(err);
}
});
d.dispose();
});
};