forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord-tests
More file actions
executable file
·185 lines (166 loc) · 5.35 KB
/
record-tests
File metadata and controls
executable file
·185 lines (166 loc) · 5.35 KB
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
#!/usr/bin/env node
'use strict';
const child_process = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
const SearchSource = require('jest-cli').SearchSource;
const TestRunner = require('jest-cli').TestRunner;
const TestWatcher = require('jest-cli/build/TestWatcher');
const createHasteContext = require('jest-runtime').createHasteContext;
const readConfig = require('jest-config').readConfig;
const argv = {};
const root = path.normalize(path.join(__dirname, '..', '..'));
const testPathPattern = '';
function wrapRunnerFile(runnerPath) {
const filename = path.join(
os.tmpdir(),
'test-runner-' + crypto.randomBytes(8).toString('hex') + '.js'
);
fs.writeFileSync(
filename,
`
'use strict';
var runnerPath = ${JSON.stringify(runnerPath)};
var wrap = require(${JSON.stringify(__filename)}).wrapRunner;
module.exports = wrap(runnerPath);
`
);
return filename;
}
function wrapRunner(originalPath) {
const original = require(originalPath);
// Assuming originalPath is .../jest-jasmine2/build/index.js
const JasmineReporter = require(
path.join(path.dirname(originalPath), 'reporter.js')
);
// For each spec, we store whether there was any expectDev() failure. This
// relies on the results being returned in the same order as they are run.
const hadDevFailures = [];
let environment;
const oldSpecStarted = JasmineReporter.prototype.specStarted;
JasmineReporter.prototype.specStarted = function(result) {
oldSpecStarted.apply(this, arguments);
environment.global.__suppressDevFailures = true;
environment.global.__hadDevFailures = false;
};
const oldSpecDone = JasmineReporter.prototype.specDone;
JasmineReporter.prototype.specDone = function(result) {
oldSpecDone.apply(this, arguments);
environment.global.__suppressDevFailures = false;
hadDevFailures.push(environment.global.__hadDevFailures);
};
return function runner(config, env, runtime, testPath) {
environment = env;
return original(config, env, runtime, testPath)
.then((results) => {
results.failureMessage = null;
hadDevFailures.forEach((hadFailures, i) => {
results.testResults[i].hadDevFailures = hadFailures;
});
hadDevFailures.length = 0;
return results;
});
};
}
function runJest(maxWorkers) {
return readConfig(argv, root)
.then((config) => {
config = Object.assign({}, config, {
testRunner: wrapRunnerFile(config.testRunner),
});
return createHasteContext(config, {}).then((hasteMap) => {
const source = new SearchSource(hasteMap, config);
return source.getTestPaths({testPathPattern})
.then((data) => {
const runner = new TestRunner(
hasteMap,
config,
{
maxWorkers: maxWorkers,
getTestSummary: () => 'You did it!'
}
);
const watcher = new TestWatcher({isWatchMode: false});
return runner.runTests(data.paths, watcher);
});
});
});
}
function formatResults(runResults, predicate) {
const formatted = [];
runResults.testResults.forEach((fileResult) => {
const file = path.relative(root, fileResult.testFilePath);
const tests = fileResult.testResults.filter(
(test) => predicate(fileResult, test)
);
if (tests.length) {
const lines = [file].concat(tests.map((test) => '* ' + test.title));
formatted.push(lines.join('\n'));
}
});
formatted.sort();
return formatted.join('\n\n');
}
function recordTests(maxWorkers, trackFacts) {
process.env.REACT_DOM_JEST_USE_FIBER = true;
runJest(maxWorkers)
.then((runResults) => {
const passing = formatResults(
runResults,
(file, test) => test.status === 'passed' && !test.hadDevFailures
);
const passingExceptDev = formatResults(
runResults,
(file, test) => test.status === 'passed' && test.hadDevFailures
);
const failing = formatResults(
runResults,
(file, test) => test.status === 'failed'
);
fs.writeFileSync(
path.join(__dirname, 'tests-passing.txt'),
passing + '\n'
);
fs.writeFileSync(
path.join(__dirname, 'tests-passing-except-dev.txt'),
passingExceptDev + '\n'
);
fs.writeFileSync(
path.join(__dirname, 'tests-failing.txt'),
failing + '\n'
);
if (trackFacts) {
const fact = runResults.numPassedTests + '/' + runResults.numTotalTests;
// TODO: Shelling out here is silly.
child_process.spawnSync(
process.execPath,
[
path.join(__dirname, '../facts-tracker/index.js'),
'fiber-tests',
fact,
],
{
stdio: 'inherit',
}
);
}
});
}
if (require.main === module) {
const argv = require('yargs')
.demand(0, 0)
.number('max-workers')
.describe('max-workers', 'Number of workers to use for jest.')
.default('max-workers', Math.max(os.cpus().length - 1, 1))
.boolean('track-facts')
.describe('track-facts', 'Use facts-tracker to record passing tests.')
.strict()
.help()
.argv;
recordTests(argv.maxWorkers, argv.trackFacts);
}
module.exports = {
wrapRunner,
};