Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80600 views
1
#!/usr/bin/env node
2
/**
3
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*/
9
/* jshint node: true */
10
'use strict';
11
12
var fs = require('fs');
13
var harmonize = require('harmonize');
14
var optimist = require('optimist');
15
var path = require('path');
16
17
/**
18
* Takes a description string, puts it on the next line, indents it, and makes
19
* sure it wraps without exceeding 80chars
20
*/
21
function _wrapDesc(desc) {
22
var indent = '\n ';
23
return indent + desc.split(' ').reduce(function(wrappedDesc, word) {
24
var lastLineIdx = wrappedDesc.length - 1;
25
var lastLine = wrappedDesc[lastLineIdx];
26
27
var appendedLastLine = lastLine === '' ? word : (lastLine + ' ' + word);
28
29
if (appendedLastLine.length > 80) {
30
wrappedDesc.push(word);
31
} else {
32
wrappedDesc[lastLineIdx] = appendedLastLine;
33
}
34
35
return wrappedDesc;
36
}, ['']).join(indent);
37
}
38
39
harmonize();
40
41
var argv = optimist
42
.usage('Usage: $0 [--config=<pathToConfigFile>] [TestPathRegExp]')
43
.options({
44
config: {
45
alias: 'c',
46
description: _wrapDesc(
47
'The path to a jest config file specifying how to find and execute ' +
48
'tests. If no rootDir is set in the config, the directory of the ' +
49
'config file is assumed to be the rootDir for the project.'
50
),
51
type: 'string'
52
},
53
coverage: {
54
description: _wrapDesc(
55
'Indicates that test coverage information should be collected and ' +
56
'reported in the output.'
57
),
58
type: 'boolean'
59
},
60
maxWorkers: {
61
alias: 'w',
62
description: _wrapDesc(
63
'Specifies the maximum number of workers the worker-pool will spawn ' +
64
'for running tests. This defaults to the number of the cores ' +
65
'available on your machine. (its usually best not to override this ' +
66
'default)'
67
),
68
type: 'string' // no, optimist -- its a number.. :(
69
},
70
onlyChanged: {
71
alias: 'o',
72
description: _wrapDesc(
73
'Attempts to identify which tests to run based on which files have ' +
74
'changed in the current repository. Only works if you\'re running ' +
75
'tests in a git repository at the moment.'
76
),
77
type: 'boolean'
78
},
79
runInBand: {
80
alias: 'i',
81
description: _wrapDesc(
82
'Run all tests serially in the current process (rather than creating ' +
83
'a worker pool of child processes that run tests). This is sometimes ' +
84
'useful for debugging, but such use cases are pretty rare.'
85
),
86
type: 'boolean'
87
},
88
testEnvData: {
89
description: _wrapDesc(
90
'A JSON object (string) that specifies data that will be made ' +
91
'available in the test environment (via jest.getEnvData())'
92
),
93
type: 'string'
94
},
95
testPathPattern: {
96
description: _wrapDesc(
97
'A regexp pattern string that is matched against all tests ' +
98
'paths before executing the test.'
99
),
100
type: 'string'
101
},
102
version: {
103
alias: 'v',
104
description: _wrapDesc('Print the version and exit'),
105
type: 'boolean'
106
},
107
noHighlight: {
108
description: _wrapDesc(
109
'Disables test results output highlighting'
110
),
111
type: 'boolean'
112
},
113
})
114
.check(function(argv) {
115
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
116
throw (
117
'Both --runInBand and --maxWorkers were specified, but these two ' +
118
'options do not make sense together. Which is it?'
119
);
120
}
121
122
if (argv.onlyChanged && argv._.length > 0) {
123
throw (
124
'Both --onlyChanged and a path pattern were specified, but these two ' +
125
'options do not make sense together. Which is it? Do you want to run ' +
126
'tests for changed files? Or for a specific set of files?'
127
);
128
}
129
130
if (argv.testEnvData) {
131
argv.testEnvData = JSON.parse(argv.testEnvData);
132
}
133
})
134
.argv;
135
136
if (argv.help) {
137
optimist.showHelp();
138
139
process.on('exit', function(){
140
process.exit(1);
141
});
142
143
return;
144
}
145
146
var cwd = process.cwd();
147
148
// Is the cwd somewhere within an npm package?
149
var cwdPackageRoot = cwd;
150
while (!fs.existsSync(path.join(cwdPackageRoot, 'package.json'))) {
151
if (cwdPackageRoot === '/') {
152
cwdPackageRoot = cwd;
153
break;
154
}
155
cwdPackageRoot = path.resolve(cwdPackageRoot, '..');
156
}
157
158
// Is there a package.json at our cwdPackageRoot that indicates that there
159
// should be a version of Jest installed?
160
var cwdPkgJsonPath = path.join(cwdPackageRoot, 'package.json');
161
162
// Is there a version of Jest installed at our cwdPackageRoot?
163
var cwdJestBinPath = path.join(cwdPackageRoot, 'node_modules/jest-cli');
164
165
// Get a jest instance
166
var jest;
167
168
if (fs.existsSync(cwdJestBinPath)) {
169
// If a version of Jest was found installed in the CWD package, use that.
170
jest = require(cwdJestBinPath);
171
172
if (!jest.runCLI) {
173
console.error(
174
'This project requires an older version of Jest than what you have ' +
175
'installed globally.\n' +
176
'Please upgrade this project past Jest version 0.1.5'
177
);
178
179
process.on('exit', function(){
180
process.exit(1);
181
});
182
183
return;
184
}
185
} else {
186
// Otherwise, load this version of Jest.
187
jest = require('../');
188
189
// If a package.json was found in the CWD package indicating a specific
190
// version of Jest to be used, bail out and ask the user to `npm install`
191
// first
192
if (fs.existsSync(cwdPkgJsonPath)) {
193
var cwdPkgJson = require(cwdPkgJsonPath);
194
var cwdPkgDeps = cwdPkgJson.dependencies;
195
var cwdPkgDevDeps = cwdPkgJson.devDependencies;
196
197
if (cwdPkgDeps && cwdPkgDeps['jest-cli']
198
|| cwdPkgDevDeps && cwdPkgDevDeps['jest-cli']) {
199
console.error(
200
'Please run `npm install` to use the version of Jest intended for ' +
201
'this project.'
202
);
203
204
process.on('exit', function(){
205
process.exit(1);
206
});
207
208
return;
209
}
210
}
211
}
212
213
if (!argv.version) {
214
console.log('Using Jest CLI v' + jest.getVersion());
215
}
216
217
jest.runCLI(argv, cwdPackageRoot, function (success) {
218
process.on('exit', function(){
219
process.exit(success ? 0 : 1);
220
});
221
});
222
223