mirror of
https://github.com/facebook/react.git
synced 2026-02-24 12:43:00 +00:00
141 lines
3.6 KiB
JavaScript
Executable File
141 lines
3.6 KiB
JavaScript
Executable File
#!/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').SearchSource;
|
|
const TestRunner = require('jest').TestRunner;
|
|
|
|
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 wrap = require(${JSON.stringify(__filename)}).wrapRunner;
|
|
var original = require(${JSON.stringify(runnerPath)});
|
|
module.exports = wrap(original);
|
|
`
|
|
);
|
|
return filename;
|
|
}
|
|
|
|
function wrapRunner(original) {
|
|
return function runner(config, environment, runtime, testPath) {
|
|
return original(config, environment, runtime, testPath)
|
|
.then((results) => {
|
|
results.failureMessage = null;
|
|
return results;
|
|
});
|
|
};
|
|
}
|
|
|
|
function runJest() {
|
|
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: Math.max(os.cpus().length - 1, 1),
|
|
getTestSummary: () => 'You did it!'
|
|
}
|
|
);
|
|
return runner.runTests(data.paths);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
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(trackFacts) {
|
|
process.env.REACT_DOM_JEST_USE_FIBER = true;
|
|
runJest()
|
|
.then((runResults) => {
|
|
const passing = formatResults(
|
|
runResults,
|
|
(file, test) => test.status === 'passed'
|
|
);
|
|
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-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)
|
|
.boolean('track-facts')
|
|
.describe('track-facts', 'Use facts-tracker to record passing tests.')
|
|
.strict()
|
|
.help()
|
|
.argv;
|
|
recordTests(argv.trackFacts);
|
|
}
|
|
|
|
module.exports = {
|
|
wrapRunner,
|
|
};
|