-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathhelpers.js
280 lines (249 loc) · 7.04 KB
/
helpers.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict';
var format = require('util').format;
var spawn = require('cross-spawn').spawn;
var path = require('path');
var baseReporter = require('../../lib/reporters/base');
var DEFAULT_FIXTURE = resolveFixturePath('__default__');
var MOCHA_EXECUTABLE = require.resolve('../../bin/mocha');
var _MOCHA_EXECUTABLE = require.resolve('../../bin/_mocha');
module.exports = {
DEFAULT_FIXTURE: DEFAULT_FIXTURE,
/**
* Invokes the mocha binary for the given fixture with color output disabled.
* Accepts an array of additional command line args to pass. The callback is
* invoked with a summary of the run, in addition to its output. The summary
* includes the number of passing, pending, and failing tests, as well as the
* exit code. Useful for testing different reporters.
*
* By default, `STDERR` is ignored. Pass `{stdio: 'pipe'}` as `opts` if you
* want it.
* Example response:
* {
* pending: 0,
* passing: 0,
* failing: 1,
* code: 1,
* output: '...'
* }
*
* @param {string} fixturePath - Path to fixture .js file
* @param {string[]} args - Extra args to mocha executable
* @param {Function} fn - Callback
* @param {Object} [opts] - Options for `spawn()`
*/
runMocha: function(fixturePath, args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
var path;
path = resolveFixturePath(fixturePath);
args = args || [];
invokeSubMocha(
args.concat(['-C', path]),
function(err, res) {
if (err) {
return fn(err);
}
fn(null, getSummary(res));
},
opts
);
},
/**
* Invokes the mocha binary for the given fixture using the JSON reporter,
* returning the parsed output, as well as exit code.
*
* By default, `STDERR` is ignored. Pass `{stdio: 'pipe'}` as `opts` if you
* want it.
* @param {string} fixturePath - Path from __dirname__
* @param {string[]} args - Array of args
* @param {Function} fn - Callback
* @param {Object} [opts] - Opts for `spawn()`
* @returns {*} Parsed object
*/
runMochaJSON: function(fixturePath, args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
var path;
path = resolveFixturePath(fixturePath);
args = args || [];
return invokeMocha(
args.concat(['--reporter', 'json', path]),
function(err, res) {
if (err) return fn(err);
try {
var result = toJSONRunResult(res);
fn(null, result);
} catch (err) {
fn(
new Error(
format(
'Failed to parse JSON reporter output from result:\n\n%O',
res
)
)
);
}
},
opts
);
},
/**
* Invokes the mocha binary for the given fixture using the JSON reporter,
* returning the **raw** string output, as well as exit code.
*
* By default, `STDERR` is ignored. Pass `{stdio: 'pipe'}` as `opts` if you
* want it.
* @param {string} fixturePath - Path from __dirname__
* @param {string[]} args - Array of args
* @param {Function} fn - Callback
* @param {Object} [opts] - Opts for `spawn()`
* @returns {string} Raw output
*/
runMochaJSONRaw: function(fixturePath, args, fn, opts) {
var path;
path = resolveFixturePath(fixturePath);
args = args || [];
return invokeSubMocha(
args.concat(['--reporter', 'json', path]),
function(err, resRaw) {
if (err) return fn(err);
fn(null, resRaw);
},
opts
);
},
/**
* regular expression used for splitting lines based on new line / dot symbol.
*/
splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'),
/**
* Invokes the mocha binary. Accepts an array of additional command line args
* to pass. The callback is invoked with the exit code and output. Optional
* current working directory as final parameter.
*
* By default, `STDERR` is ignored. Pass `{stdio: 'pipe'}` as `opts` if you
* want it.
*
* In most cases runMocha should be used instead.
*
* Example response:
* {
* code: 1,
* output: '...'
* }
*
* @param {Array<string>} args - Extra args to mocha executable
* @param {Function} done - Callback
* @param {Object} [opts] - Options for `spawn()`
*/
invokeMocha: invokeMocha,
/**
* Resolves the path to a fixture to the full path.
*/
resolveFixturePath: resolveFixturePath,
toJSONRunResult: toJSONRunResult
};
/**
* Coerce output as returned by _spawnMochaWithListeners using JSON reporter into a JSONRunResult as
* recognized by our custom unexpected assertions
* @param {string} result - Raw stdout from Mocha run using JSON reporter
* @private
*/
function toJSONRunResult(result) {
var code = result.code;
result = JSON.parse(result.output);
result.code = code;
return result;
}
/**
* Creates arguments loading a default fixture if none provided
*
* @param {string[]|*} [args] - Arguments to `spawn`
* @returns string[]
*/
function defaultArgs(args) {
return !args || !args.length ? ['--file', DEFAULT_FIXTURE] : args;
}
function invokeMocha(args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
return _spawnMochaWithListeners(
defaultArgs([MOCHA_EXECUTABLE].concat(args)),
fn,
opts
);
}
function invokeSubMocha(args, fn, opts) {
if (typeof args === 'function') {
opts = fn;
fn = args;
args = [];
}
return _spawnMochaWithListeners(
defaultArgs([_MOCHA_EXECUTABLE].concat(args)),
fn,
opts
);
}
/**
* Spawns Mocha in a subprocess and returns an object containing its output and exit code
*
* @param {string[]} args - Path to executable and arguments
* @param {Function} fn - Callback
* @param {Object|string} [opts] - Options to `cross-spawn`, or 'pipe' for shortcut to `{stdio: pipe}`
* @returns {ChildProcess}
* @private
*/
function _spawnMochaWithListeners(args, fn, opts) {
var output = '';
if (opts === 'pipe') {
opts = {stdio: 'pipe'};
}
opts = Object.assign(
{
cwd: process.cwd(),
stdio: ['ignore', 'pipe', 'ignore']
},
opts || {}
);
var mocha = spawn(process.execPath, args, opts);
var listener = function(data) {
output += data;
};
mocha.stdout.on('data', listener);
if (mocha.stderr) {
mocha.stderr.on('data', listener);
}
mocha.on('error', fn);
mocha.on('close', function(code) {
fn(null, {
output: output.split('\n').join('\n'),
code: code
});
});
return mocha;
}
function resolveFixturePath(fixture) {
if (path.extname(fixture) !== '.js') {
fixture += '.fixture.js';
}
return path.join('test', 'integration', 'fixtures', fixture);
}
function getSummary(res) {
return ['passing', 'pending', 'failing'].reduce(function(summary, type) {
var pattern, match;
pattern = new RegExp(' (\\d+) ' + type + '\\s');
match = pattern.exec(res.output);
summary[type] = match ? parseInt(match, 10) : 0;
return summary;
}, res);
}