Skip to content

Commit

Permalink
Restore coverage in Travis (facebook#7628)
Browse files Browse the repository at this point in the history
We disabled coverage in Travis because the implementation was crashing ( facebook#6290 ). Since we upgraded to Jest 15, the entire coverage implementation is brand new so we should give it another try.
  • Loading branch information
vjeux authored Sep 2, 2016
1 parent 31dd694 commit 839697f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 90 deletions.
10 changes: 2 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ script:
fi
elif [ "$TEST_TYPE" = test ]; then
set -e
# Disabling coverage because it's broken:
# https://travis-ci.org/facebook/react/jobs/128163922
if false; then
./node_modules/.bin/grunt jest:coverage
cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
else
./node_modules/.bin/grunt jest:normal
fi
./node_modules/.bin/grunt jest:coverage
cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
echo 'Testing in server-render (HTML generation) mode...'
printf '\nmodule.exports.useCreateElement = false;\n' \
>> src/renderers/dom/shared/ReactDOMFeatureFlags.js
Expand Down
87 changes: 6 additions & 81 deletions grunt/tasks/jest.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,16 @@
'use strict';

var async = require('async');
var fs = require('fs');
var glob = require('glob');
var grunt = require('grunt');
var path = require('path');

var rootPath = path.resolve('.');
var buildPath = path.join(rootPath, 'build');
var tempConfigPath = path.join(buildPath, 'jest-config.json');

var config = require(path.join(rootPath, 'package.json')).jest;

var collectCoverageOnlyFrom = {
'src/**/*.js': {
ignore: [
'src/**/__tests__/*.js',
'src/shared/vendor/third_party/*.js',
'src/test/*.js',
],
},
};

function getCollectCoverageOnlyFrom(callback) {
var patterns = Object.keys(collectCoverageOnlyFrom);
var result = {};

async.each(patterns, function(pattern) {
var options = Object.assign({ nodir: true }, collectCoverageOnlyFrom[pattern]);
glob(pattern, options, function(err, files) {
(files || []).reduce(function(object, key) {
object[key] = true;
return object;
}, result);

callback(err);
});
}, function(err) {
callback(err, result);
});
}

function getJestConfig(callback) {
var rootDir = path.resolve(buildPath, path.resolve(config.rootDir));
getCollectCoverageOnlyFrom(function(err, data) {
callback(err, Object.assign({}, config, {
rootDir: rootDir,
name: 'react',
collectCoverage: true,
collectCoverageOnlyFrom: data,
}));
});
}

function onError(err) {
grunt.log.error('jest failed');
grunt.log.error(err);
}

function writeTempConfig(callback) {
getJestConfig(function(err, data) {
if (err) {
callback(err);
} else {
grunt.file.mkdir(buildPath);
fs.writeFile(tempConfigPath, JSON.stringify(data, null, ' '), 'utf8', callback);
}
});
}

function run(done, configPath) {
function run(done, coverage) {
grunt.log.writeln('running jest');

var args = [
path.join('node_modules', 'jest', 'bin', 'jest'),
'--runInBand',
'--no-watchman',
];
if (configPath) {
args.push('--config', configPath);
if (coverage) {
args.push('--coverage');
}
grunt.util.spawn({
cmd: 'node',
Expand All @@ -91,7 +23,8 @@ function run(done, configPath) {
},
}, function(spawnErr, result, code) {
if (spawnErr) {
onError(spawnErr);
grunt.log.error('jest failed');
grunt.log.error(spawnErr);
} else {
grunt.log.ok('jest passed');
}
Expand All @@ -108,15 +41,7 @@ function runJestNormally() {

function runJestWithCoverage() {
var done = this.async();

writeTempConfig(function(writeErr) {
if (writeErr) {
onError(writeErr);
return;
}

run(done, tempConfigPath);
});
run(done, /* coverage */ true);
}

module.exports = {
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"gulp-load-plugins": "^1.2.4",
"gulp-util": "^3.0.7",
"gzip-js": "~0.3.2",
"jest": "^15.0.1",
"jest": "^15.0.2",
"loose-envify": "^1.1.0",
"merge-stream": "^1.0.0",
"object-assign": "^4.1.0",
Expand Down Expand Up @@ -111,6 +111,12 @@
"<rootDir>/src",
"node_modules/fbjs"
],
"collectCoverageFrom": [
"src/**/*.js",
"!src/**/__tests__/*.js",
"!src/shared/vendor/third_party/*.js",
"!src/test/*.js"
],
"timers": "fake"
}
}

0 comments on commit 839697f

Please sign in to comment.