Skip to content

Commit

Permalink
restore generateEmptyCoverage
Browse files Browse the repository at this point in the history
rely on babel for instrumentation
  • Loading branch information
jwbay committed Dec 17, 2016
1 parent 6551e0a commit d18c876
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
30 changes: 30 additions & 0 deletions packages/jest-cli/src/generateEmptyCoverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2014-present, 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.
*
* @flow
*/

'use strict';

import type {Config, Path} from 'types/Config';

const IstanbulInstrument = require('istanbul-lib-instrument');

const {transformSource, shouldInstrument} = require('jest-runtime');

module.exports = function(source: string, filename: Path, config: Config) {
if (shouldInstrument(filename, config)) {
// Transform file without instrumentation first, to make sure produced
// source code is ES6 (no flowtypes etc.) and can be instrumented
source = transformSource(filename, config, source, false);
const instrumenter = IstanbulInstrument.createInstrumenter();
instrumenter.instrumentSync(source, filename);
return instrumenter.fileCoverage;
} else {
return null;
}
};
8 changes: 2 additions & 6 deletions packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import type {RunnerContext} from 'types/Reporters';
const BaseReporter = require('./BaseReporter');

const {clearLine} = require('jest-util');
const {generateEmptyCoverageForSource} = require('jest-runtime');
const {createReporter} = require('istanbul-api');
const chalk = require('chalk');
const fs = require('fs');
const generateEmptyCoverage = require('../generateEmptyCoverage');
const isCI = require('is-ci');
const istanbulCoverage = require('istanbul-lib-coverage');
const libSourceMaps = require('istanbul-lib-source-maps');
Expand Down Expand Up @@ -104,11 +104,7 @@ class CoverageReporter extends BaseReporter {
if (!this._coverageMap.data[filename]) {
try {
const source = fs.readFileSync(filename).toString();
const coverage = generateEmptyCoverageForSource(
filename,
config,
source
);
const coverage = generateEmptyCoverage(source, filename, config);
if (coverage) {
this._coverageMap.addFileCoverage(coverage);
}
Expand Down
1 change: 0 additions & 1 deletion packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"babel-plugin-istanbul": "^3.0.0",
"chalk": "^1.1.3",
"graceful-fs": "^4.1.6",
"istanbul-lib-instrument": "^1.3.0",
"jest-config": "^18.0.0",
"jest-file-exists": "^17.0.0",
"jest-haste-map": "^18.0.0",
Expand Down
8 changes: 0 additions & 8 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,6 @@ class Runtime {
return transform.transformSource(filename, config, content, instrument);
}

static generateEmptyCoverageForSource(
filename: Path,
config: Config,
content: string,
) {
return transform.generateEmptyCoverage(filename, config, content);
}

static createHasteContext(
config: Config,
options: {
Expand Down
29 changes: 0 additions & 29 deletions packages/jest-runtime/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const crypto = require('crypto');
const fileExists = require('jest-file-exists');
const fs = require('graceful-fs');
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
const IstanbulInstrument = require('istanbul-lib-instrument');
const path = require('path');
const shouldInstrument = require('./shouldInstrument');
const stableStringify = require('json-stable-stringify');
Expand Down Expand Up @@ -298,33 +297,6 @@ const transformSource = (
return result;
};

const generateEmptyCoverage = (
filename: Path,
config: Config,
content: string
): Object => {
const transform = getTransformer(filename, config);
let source = content;

if (transform && shouldTransform(filename, config)) {
if (
transform.canInstrument &&
typeof transform.getEmptyCoverage === 'function'
) {
return transform.getEmptyCoverage(content, filename, config, {
instrument: true,
watch: config.watch,
});
} else {
source = transformSource(filename, config, content, false);
}
}

const instrumenter = IstanbulInstrument.createInstrumenter();
instrumenter.instrumentSync(source, filename);
return instrumenter.lastFileCoverage();
};

const transformAndBuildScript = (
filename: Path,
config: Config,
Expand Down Expand Up @@ -384,4 +356,3 @@ module.exports = (

module.exports.EVAL_RESULT_VARIABLE = EVAL_RESULT_VARIABLE;
module.exports.transformSource = transformSource;
module.exports.generateEmptyCoverage = generateEmptyCoverage;

0 comments on commit d18c876

Please sign in to comment.