-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
index.js
77 lines (66 loc) · 1.81 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
74
75
76
77
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_OPTIONS = new Set([
'require',
]);
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,
};
for (const [key, value] of Object.entries(options)) {
if (Array.isArray(value)) {
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] = convertObjectToList(value);
}
}
const arguments_ = dargs(options, {
excludes: ['suppress'],
ignoreFalse: true,
});
const files = [];
return gulpPlugin('gulp-mocha', file => {
files.push(file.path);
}, {
supportsAnyType: true,
async * onFinish(stream) { // eslint-disable-line require-yield
const subprocess = execa('mocha', [...files, ...arguments_], {
localDir: __dirname,
preferLocal: true,
});
if (!options.suppress) {
subprocess.stdout.pipe(process.stdout);
subprocess.stderr.pipe(process.stderr);
}
try {
const result = await subprocess;
stream.emit('_result', result);
} catch (error) {
if (error.exitCode > 0) {
const error = new Error('There were test failures');
error.isPresentable = true;
throw error;
}
throw error;
}
},
});
}