/
jest.js
executable file
·395 lines (348 loc) · 11.7 KB
/
jest.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env node
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* jshint node: true */
"use strict";
var child_process = require('child_process');
var defaultTestResultHandler = require('../src/defaultTestResultHandler');
var fs = require('fs');
var harmonize = require('harmonize');
var optimist = require('optimist');
var path = require('path');
var Q = require('q');
var TestRunner = require('../src/TestRunner');
var colors = require('../src/lib/colors');
var utils = require('../src/lib/utils');
var _jestVersion = null;
function _getJestVersion() {
if (_jestVersion === null) {
var pkgJsonPath = path.resolve(__dirname, '..', 'package.json');
_jestVersion = require(pkgJsonPath).version;
}
return _jestVersion;
}
function _findChangedFiles(dirPath) {
var deferred = Q.defer();
var args =
['diff', '--name-only', '--diff-filter=ACMR'];
var child = child_process.spawn('git', args, {cwd: dirPath});
var stdout = '';
child.stdout.on('data', function(data) {
stdout += data;
});
var stderr = '';
child.stderr.on('data', function(data) {
stderr += data;
});
child.on('close', function(code) {
if (code === 0) {
stdout = stdout.trim();
if (stdout === '') {
deferred.resolve([]);
} else {
deferred.resolve(stdout.split('\n').map(function(changedPath) {
return path.resolve(dirPath, changedPath);
}));
}
} else {
deferred.reject(code + ': ' + stderr);
}
});
return deferred.promise;
}
function _onResultReady(config, result) {
return defaultTestResultHandler(config, result);
}
function _onRunComplete(completionData) {
var numFailedTests = completionData.numFailedTests;
var numTotalTests = completionData.numTotalTests;
var numPassedTests = numTotalTests - numFailedTests;
var startTime = completionData.startTime;
var endTime = completionData.endTime;
var results = '';
if (numFailedTests) {
results +=
colors.colorize(
[numFailedTests, (numFailedTests > 1 ? 'tests' : 'test'), 'failed'].join(' '),
colors.RED + colors.BOLD
);
results += ', ';
}
results +=
colors.colorize(
[numPassedTests, (numPassedTests > 1 ? 'tests' : 'test'), 'passed'].join(' '),
colors.GREEN + colors.BOLD
);
results += ' (' + numTotalTests + ' total)';
console.log(results);
console.log('Run time: ' + ((endTime - startTime) / 1000) + 's');
}
function _verifyIsGitRepository(dirPath) {
var deferred = Q.defer();
child_process.spawn('git', ['rev-parse', '--git-dir'], {cwd: dirPath})
.on('close', function(code) {
var isGitRepo = code === 0;
deferred.resolve(isGitRepo);
});
return deferred.promise;
}
/**
* Takes a description string, puts it on the next line, indents it, and makes
* sure it wraps without exceeding 80chars
*/
function _wrapDesc(desc) {
var indent = '\n ';
return indent + desc.split(' ').reduce(function(wrappedDesc, word) {
var lastLineIdx = wrappedDesc.length - 1;
var lastLine = wrappedDesc[lastLineIdx];
var appendedLastLine = lastLine === '' ? word : (lastLine + ' ' + word);
if (appendedLastLine.length > 80) {
wrappedDesc.push(word);
} else {
wrappedDesc[lastLineIdx] = appendedLastLine;
}
return wrappedDesc;
}, ['']).join(indent);
}
function runCLI(argv, packageRoot, onComplete) {
argv = argv || {};
if (argv.version) {
console.log('v' + _getJestVersion());
onComplete && onComplete(true);
return;
}
var config;
if (argv.config) {
if (typeof argv.config === 'string') {
config = utils.loadConfigFromFile(argv.config);
} else if (typeof argv.config === 'object') {
config = Q(utils.normalizeConfig(argv.config));
}
} else {
var pkgJsonPath = path.join(packageRoot, 'package.json');
var pkgJson = fs.existsSync(pkgJsonPath) ? require(pkgJsonPath) : {};
// First look to see if there is a package.json file with a jest config in it
if (pkgJson.jest) {
if (!pkgJson.jest.hasOwnProperty('rootDir')) {
pkgJson.jest.rootDir = packageRoot;
} else {
pkgJson.jest.rootDir = path.resolve(packageRoot, pkgJson.jest.rootDir);
}
config = utils.normalizeConfig(pkgJson.jest);
config.name = pkgJson.name;
config = Q(config);
// If not, use a sane default config
} else {
config = Q(utils.normalizeConfig({
name: packageRoot.replace(/[/\\]/g, '_'),
rootDir: packageRoot,
testPathDirs: [packageRoot],
testPathIgnorePatterns: ['/node_modules/.+']
}));
}
}
config.done(function(config) {
var pathPattern =
argv.testPathPattern ||
(argv._ && argv._.length ? new RegExp(argv._.join('|')) : /.*/);
var testRunnerOpts = {};
if (argv.maxWorkers) {
testRunnerOpts.maxWorkers = argv.maxWorkers;
}
if (argv.coverage) {
config.collectCoverage = true;
}
var testRunner = new TestRunner(config, testRunnerOpts);
function _runTestsOnPathPattern(pathPattern) {
return testRunner.findTestPathsMatching(pathPattern)
.then(function(matchingTestPaths) {
var numMatchingTestPaths = matchingTestPaths.length;
console.log(
['Found', numMatchingTestPaths, 'matching', (numMatchingTestPaths > 1 ? 'tests...' : 'test...')].join(' ')
);
if (argv.runInBand) {
return testRunner.runTestsInBand(matchingTestPaths, _onResultReady);
} else {
return testRunner.runTestsParallel(matchingTestPaths, _onResultReady);
}
})
.then(function(completionData) {
_onRunComplete(completionData);
onComplete && onComplete(completionData.numFailedTests === 0);
});
}
if (argv.onlyChanged) {
console.log('Looking for changed files...');
var testPathDirsAreGit = config.testPathDirs.map(_verifyIsGitRepository);
Q.all(testPathDirsAreGit).then(function(results) {
if (!results.every(function(result) { return result; })) {
console.error(
'It appears that one of your testPathDirs does not exist ' +
'with in a git repository. Currently --onlyChanged only works ' +
'with git projects.\n'
);
onComplete(false);
}
return Q.all(config.testPathDirs.map(_findChangedFiles));
}).then(function(changedPathSets) {
// Collapse changed files from each of the testPathDirs into a single list
// of changed file paths
var changedPaths = [];
changedPathSets.forEach(function(pathSet) {
changedPaths = changedPaths.concat(pathSet);
});
return testRunner.findTestsRelatedTo(changedPaths);
}).done(function(affectedTestPaths) {
if (affectedTestPaths.length > 0) {
_runTestsOnPathPattern(new RegExp(affectedTestPaths.join('|'))).done();
} else {
console.log('No tests to run!');
}
});
} else {
_runTestsOnPathPattern(pathPattern).done();
}
});
}
function _main(onComplete) {
var argv = optimist
.usage('Usage: $0 [--config=<pathToConfigFile>] [TestPathRegExp]')
.options({
config: {
alias: 'c',
description: _wrapDesc(
'The path to a jest config file specifying how to find and execute ' +
'tests.'
),
type: 'string'
},
coverage: {
description: _wrapDesc(
'Indicates that test coverage information should be collected and ' +
'reported in the output.'
),
type: 'boolean'
},
maxWorkers: {
alias: 'w',
description: _wrapDesc(
'Specifies the maximum number of workers the worker-pool will spawn ' +
'for running tests. This defaults to the number of the cores ' +
'available on your machine. (its usually best not to override this ' +
'default)'
),
type: 'string' // no, optimist -- its a number.. :(
},
onlyChanged: {
alias: 'o',
description: _wrapDesc(
'Attempts to identify which tests to run based on which files have ' +
'changed in the current repository. Only works if you\'re running ' +
'tests in a git repository at the moment.'
),
type: 'boolean'
},
runInBand: {
alias: 'i',
description: _wrapDesc(
'Run all tests serially in the current process (rather than creating ' +
'a worker pool of child processes that run tests). This is sometimes ' +
'useful for debugging, but such use cases are pretty rare.'
),
type: 'boolean'
},
version: {
alias: 'v',
description: _wrapDesc('Print the version and exit'),
type: 'boolean'
}
})
.check(function(argv) {
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
throw (
"Both --runInBand and --maxWorkers were specified, but these two " +
"options don't make sense together. Which is it?"
);
}
if (argv.onlyChanged && argv._.length > 0) {
throw (
"Both --onlyChanged and a path pattern were specified, but these two " +
"options don't make sense together. Which is it? Do you want to run " +
"tests for changed files? Or for a specific set of files?"
);
}
})
.argv
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
var cwd = process.cwd();
// Is the cwd somewhere within an npm package?
var cwdPackageRoot = cwd;
while (!fs.existsSync(path.join(cwdPackageRoot, 'package.json'))) {
if (cwdPackageRoot === '/') {
cwdPackageRoot = cwd;
break;
}
cwdPackageRoot = path.resolve(cwdPackageRoot, '..');
}
// Is there a package.json at our cwdPackageRoot that indicates that there
// should be a version of Jest installed?
var cwdPkgJsonPath = path.join(cwdPackageRoot, 'package.json');
// Is there a version of Jest installed at our cwdPackageRoot?
var cwdJestBinPath = path.join(
cwdPackageRoot,
'node_modules',
'jest-cli',
'bin',
'jest.js'
);
// If a version of Jest was found installed in the CWD package, run using that
if (fs.existsSync(cwdJestBinPath)) {
var jestBinary = require(cwdJestBinPath);
if (!jestBinary.runCLI) {
console.error(
'This project requires an older version of Jest than what you have ' +
'installed globally.\n' +
'Please upgrade this project past Jest version 0.1.5'
);
process.exit(1);
}
jestBinary.runCLI(argv, cwdPackageRoot, onComplete);
return;
}
// If a package.json was found in the CWD package indicating a specific
// version of Jest to be used, bail out and ask the user to `npm install`
// first
if (fs.existsSync(cwdPkgJsonPath)) {
var cwdPkgJson = require(cwdPkgJsonPath);
var cwdPkgDeps = cwdPkgJson.dependencies;
var cwdPkgDevDeps = cwdPkgJson.devDependencies;
var thisJestVersion = _getJestVersion();
if (cwdPkgDeps && cwdPkgDeps['jest-cli']
|| cwdPkgDevDeps && cwdPkgDevDeps['jest-cli']) {
console.error(
'Please run `npm install` to use the version of Jest intended for ' +
'this project.'
);
process.exit(1);
}
}
if (!argv.version && cwdPackageRoot) {
console.log('Using Jest CLI v' + _getJestVersion());
}
runCLI(argv, cwdPackageRoot, onComplete);
}
exports.runCLI = runCLI;
if (require.main === module) {
harmonize();
_main(function (success) {
process.exit(success ? 0 : 1);
});
}