| 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 |
71
71
71
71
7
636
248
248
175
176
73
73
73
441
73
383
356
27
5
7
20
7
7
7
37
37
1
36
36
72
72
3
5
5
5
3
13
5
13
9
9
6
12
6
40
40
1
71
71
35
385
366
385
35
7
108
7
7
71
7
27
3
24
24
24
24
7
27
27
27
27
189
27
27
71
71
71
71
71
71
71
71
71
| /**
* 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.
*/
'use strict';
var fs = require('graceful-fs');
var path = require('path');
var Q = require('q');
var DEFAULT_CONFIG_VALUES = {
cacheDirectory: path.resolve(__dirname, '..', '..', '.haste_cache'),
globals: {},
moduleLoader: require.resolve('../HasteModuleLoader/HasteModuleLoader'),
modulePathIgnorePatterns: [],
testDirectoryName: '__tests__',
testEnvironment: require.resolve('../JSDomEnvironment'),
testFileExtensions: ['js'],
moduleFileExtensions: ['js', 'json'],
testPathDirs: ['<rootDir>'],
testPathIgnorePatterns: ['/node_modules/'],
testRunner: require.resolve('../jasmineTestRunner/jasmineTestRunner'),
};
function _replaceRootDirTags(rootDir, config) {
switch (typeof config) {
case 'object':
Iif (config instanceof RegExp) {
return config;
}
if (Array.isArray(config)) {
return config.map(function(item) {
return _replaceRootDirTags(rootDir, item);
});
}
Eif (config !== null) {
var newConfig = {};
for (var configKey in config) {
newConfig[configKey] =
configKey === 'rootDir'
? config[configKey]
: _replaceRootDirTags(rootDir, config[configKey]);
}
return newConfig;
}
break;
case 'string':
if (!/^<rootDir>/.test(config)) {
return config;
}
return pathNormalize(path.resolve(
rootDir,
'./' + path.normalize(config.substr('<rootDir>'.length))
));
}
return config;
}
function escapeStrForRegex(str) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
/**
* Given the coverage info for a single file (as output by
* CoverageCollector.js), return an array whose entries are bools indicating
* whether anything on the line could have been covered and was, or null if the
* line wasn't measurable (like empty lines, declaration keywords, etc).
*
* For example, for the following coverage info:
*
* COVERED: var a = [];
* NO CODE:
* COVERED: for (var i = 0; i < a.length; i++)
* NOT COVERED: console.log('hai!');
*
* You'd get an array that looks like this:
*
* [true, null, true, false]
*/
function getLineCoverageFromCoverageInfo(coverageInfo) {
var coveredLines = {};
coverageInfo.coveredSpans.forEach(function(coveredSpan) {
var startLine = coveredSpan.start.line;
var endLine = coveredSpan.end.line;
for (var i = startLine - 1; i < endLine; i++) {
coveredLines[i] = true;
}
});
var uncoveredLines = {};
coverageInfo.uncoveredSpans.forEach(function(uncoveredSpan) {
var startLine = uncoveredSpan.start.line;
var endLine = uncoveredSpan.end.line;
for (var i = startLine - 1; i < endLine; i++) {
uncoveredLines[i] = true;
}
});
var sourceLines = coverageInfo.sourceText.trim().split('\n');
return sourceLines.map(function(line, lineIndex) {
if (uncoveredLines[lineIndex] === true) {
return false;
} else if (coveredLines[lineIndex] === true) {
return true;
} else {
return null;
}
});
}
/**
* Given the coverage info for a single file (as output by
* CoverageCollector.js), return the decimal percentage of lines in the file
* that had any coverage info.
*
* For example, for the following coverage info:
*
* COVERED: var a = [];
* NO CODE:
* COVERED: for (var i = 0; i < a.length; i++)
* NOT COVERED: console.log('hai');
*
* You'd get: 2/3 = 0.666666
*/
function getLinePercentCoverageFromCoverageInfo(coverageInfo) {
var lineCoverage = getLineCoverageFromCoverageInfo(coverageInfo);
var numMeasuredLines = 0;
var numCoveredLines = lineCoverage.reduce(function(counter, lineIsCovered) {
if (lineIsCovered !== null) {
numMeasuredLines++;
if (lineIsCovered === true) {
counter++;
}
}
return counter;
}, 0);
return numCoveredLines / numMeasuredLines;
}
function normalizeConfig(config) {
var newConfig = {};
// Assert that there *is* a rootDir
if (!config.hasOwnProperty('rootDir')) {
throw new Error('No rootDir config value found!');
}
config.rootDir = pathNormalize(config.rootDir);
// Normalize user-supplied config options
Object.keys(config).reduce(function(newConfig, key) {
var value;
switch (key) {
case 'collectCoverageOnlyFrom':
value = Object.keys(config[key]).reduce(function(normObj, filePath) {
filePath = pathNormalize(path.resolve(
config.rootDir,
_replaceRootDirTags(config.rootDir, filePath)
));
normObj[filePath] = true;
return normObj;
}, {});
break;
case 'testPathDirs':
value = config[key].map(function(scanDir) {
return pathNormalize(path.resolve(
config.rootDir,
_replaceRootDirTags(config.rootDir, scanDir)
));
});
break;
case 'cacheDirectory':
case 'scriptPreprocessor':
case 'setupEnvScriptFile':
case 'setupTestFrameworkScriptFile':
value = pathNormalize(path.resolve(
config.rootDir,
_replaceRootDirTags(config.rootDir, config[key])
));
break;
case 'testPathIgnorePatterns':
case 'modulePathIgnorePatterns':
case 'unmockedModulePathPatterns':
// _replaceRootDirTags is specifically well-suited for substituting
// <rootDir> in paths (it deals with properly interpreting relative path
// separators, etc).
//
// For patterns, direct global substitution is far more ideal, so we
// special case substitutions for patterns here.
value = config[key].map(function(pattern) {
return pattern.replace(/<rootDir>/g, config.rootDir);
});
break;
case 'collectCoverage':
case 'globals':
case 'moduleLoader':
case 'name':
case 'persistModuleRegistryBetweenSpecs':
case 'rootDir':
case 'setupJSTestLoaderOptions':
case 'testDirectoryName':
case 'testFileExtensions':
case 'moduleFileExtensions':
value = config[key];
break;
default:
throw new Error('Unknown config option: ' + key);
}
newConfig[key] = value;
return newConfig;
}, newConfig);
// If any config entries weren't specified but have default values, apply the
// default values
Object.keys(DEFAULT_CONFIG_VALUES).reduce(function(newConfig, key) {
if (!newConfig[key]) {
newConfig[key] = DEFAULT_CONFIG_VALUES[key];
}
return newConfig;
}, newConfig);
return _replaceRootDirTags(newConfig.rootDir, newConfig);
}
function pathNormalize(dir) {
return path.normalize(dir.replace(/\\/g, '/')).replace(/\\/g, '/');
}
function loadConfigFromFile(filePath) {
var fileDir = path.dirname(filePath);
return Q.nfcall(fs.readFile, filePath, 'utf8').then(function(fileData) {
var config = JSON.parse(fileData);
if (!config.hasOwnProperty('rootDir')) {
config.rootDir = fileDir;
} else {
config.rootDir = path.resolve(fileDir, config.rootDir);
}
return normalizeConfig(config);
});
}
function loadConfigFromPackageJson(filePath) {
var pkgJsonDir = path.dirname(filePath);
return Q.nfcall(fs.readFile, filePath, 'utf8').then(function(fileData) {
var packageJsonData = JSON.parse(fileData);
var config = packageJsonData.jest;
config.name = packageJsonData.name;
if (!config.hasOwnProperty('rootDir')) {
config.rootDir = pkgJsonDir;
} else {
config.rootDir = path.resolve(pkgJsonDir, config.rootDir);
}
return normalizeConfig(config);
});
}
var _contentCache = {};
function readAndPreprocessFileContent(filePath, config) {
if (_contentCache.hasOwnProperty(filePath)) {
return _contentCache[filePath];
}
var fileData = fs.readFileSync(filePath, 'utf8');
// If the file data starts with a shebang remove it (but leave the line empty
// to keep stack trace line numbers correct)
Iif (fileData.substr(0, 2) === '#!') {
fileData = fileData.replace(/^#!.*/, '');
}
Iif (config.scriptPreprocessor) {
try {
fileData = require(config.scriptPreprocessor).process(fileData, filePath);
} catch (e) {
e.message = config.scriptPreprocessor + ': ' + e.message;
throw e;
}
}
return _contentCache[filePath] = fileData;
}
function runContentWithLocalBindings(contextRunner, scriptContent, scriptPath,
bindings) {
var boundIdents = Object.keys(bindings);
try {
var wrapperFunc = contextRunner(
'(function(' + boundIdents.join(',') + '){' +
scriptContent +
'\n})',
scriptPath
);
} catch (e) {
e.message = scriptPath + ': ' + e.message;
throw e;
}
var bindingValues = boundIdents.map(function(ident) {
return bindings[ident];
});
try {
wrapperFunc.apply(null, bindingValues);
} catch (e) {
e.message = scriptPath + ': ' + e.message;
throw e;
}
}
exports.escapeStrForRegex = escapeStrForRegex;
exports.getLineCoverageFromCoverageInfo = getLineCoverageFromCoverageInfo;
exports.getLinePercentCoverageFromCoverageInfo =
getLinePercentCoverageFromCoverageInfo;
exports.loadConfigFromFile = loadConfigFromFile;
exports.loadConfigFromPackageJson = loadConfigFromPackageJson;
exports.normalizeConfig = normalizeConfig;
exports.pathNormalize = pathNormalize;
exports.readAndPreprocessFileContent = readAndPreprocessFileContent;
exports.runContentWithLocalBindings = runContentWithLocalBindings;
|