Skip to content

Commit

Permalink
Coverage mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Apr 27, 2017
1 parent 952479d commit 2e9da86
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
13 changes: 9 additions & 4 deletions packages/jest-cli/src/__tests__/generateEmptyCoverage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ module.exports = {

it('generates an empty coverage object for a file without running it', () => {
expect(
generateEmptyCoverage(src, '/sum.js', {}, {
cacheDirectory: os.tmpdir(),
rootDir: os.tmpdir(),
}).coverage,
generateEmptyCoverage(
src,
'/sum.js',
{},
{
cacheDirectory: os.tmpdir(),
rootDir: os.tmpdir(),
},
).coverage,
).toMatchSnapshot();
});
2 changes: 2 additions & 0 deletions packages/jest-cli/src/generateEmptyCoverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function(
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
mapCoverage: globalConfig.mapCoverage,
};
if (shouldInstrument(filename, coverageOptions, config)) {
// Transform file without instrumentation first, to make sure produced
Expand All @@ -34,6 +35,7 @@ module.exports = function(
filename,
source,
false,
globalConfig.mapCoverage,
);
const instrumenter = IstanbulInstrument.createInstrumenter();
instrumenter.instrumentSync(transformResult.code, filename);
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {TestResult} from 'types/TestResult';
import type {Resolver} from 'types/Resolve';
import type RuntimeClass from 'jest-runtime';

const BufferedConsole = require('./lib/BufferedConsole');
const {Console, NullConsole, setGlobal} = require('jest-util');
Expand Down Expand Up @@ -52,7 +53,9 @@ function runTest(
/* $FlowFixMe */
const TestRunner = require(config.testRunner);
/* $FlowFixMe */
const Runtime = require(config.moduleLoader || 'jest-runtime');
const Runtime = (require(config.moduleLoader || 'jest-runtime'): Class<
RuntimeClass
>);

const env = new TestEnvironment(config);
const TestConsole = globalConfig.verbose
Expand All @@ -75,6 +78,7 @@ function runTest(
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
mapCoverage: globalConfig.mapCoverage,
});
const start = Date.now();
return TestRunner(globalConfig, config, env, runtime, path)
Expand Down
36 changes: 30 additions & 6 deletions packages/jest-runtime/src/ScriptTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type Options = {|
collectCoverageFrom: Array<Glob>,
collectCoverageOnlyFrom: ?{[key: string]: boolean},
isInternalModule?: boolean,
mapCoverage: boolean,
|};

const cache: Map<string, TransformResult> = new Map();
Expand All @@ -50,7 +51,12 @@ class ScriptTransformer {
this._transformCache = new Map();
}

_getCacheKey(fileData: string, filename: Path, instrument: boolean): string {
_getCacheKey(
fileData: string,
filename: Path,
instrument: boolean,
mapCoverage: boolean,
): string {
if (!configToJsonMap.has(this._config)) {
// We only need this set of config options that can likely influence
// cached output instead of all config options.
Expand All @@ -69,6 +75,7 @@ class ScriptTransformer {
.update(fileData)
.update(configString)
.update(instrument ? 'instrument' : '')
.update(mapCoverage ? 'mapCoverage' : '')
.digest('hex');
}
}
Expand All @@ -77,13 +84,19 @@ class ScriptTransformer {
filename: Path,
content: string,
instrument: boolean,
mapCoverage: boolean,
): Path {
const baseCacheDir = getCacheFilePath(
this._config.cacheDirectory,
'jest-transform-cache-' + this._config.name,
VERSION,
);
const cacheKey = this._getCacheKey(content, filename, instrument);
const cacheKey = this._getCacheKey(
content,
filename,
instrument,
mapCoverage,
);
// Create sub folders based on the cacheKey to avoid creating one
// directory with many files.
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
Expand Down Expand Up @@ -160,9 +173,19 @@ class ScriptTransformer {
}).code;
}

transformSource(filename: Path, content: string, instrument: boolean) {
transformSource(
filename: Path,
content: string,
instrument: boolean,
mapCoverage: boolean,
) {
const transform = this._getTransformer(filename);
const cacheFilePath = this._getFileCachePath(filename, content, instrument);
const cacheFilePath = this._getFileCachePath(
filename,
content,
instrument,
mapCoverage,
);
let sourceMapPath = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
let code = this._config.cache
Expand Down Expand Up @@ -193,7 +216,7 @@ class ScriptTransformer {
}
}

if (this._config.mapCoverage) {
if (mapCoverage) {
if (!transformed.map) {
const convert = require('convert-source-map');
const inlineSourceMap = convert.fromSource(transformed.code);
Expand All @@ -215,7 +238,7 @@ class ScriptTransformer {
code = transformed.code;
}

if (instrument && transformed.map && this._config.mapCoverage) {
if (instrument && transformed.map && mapCoverage) {
const sourceMapContent = typeof transformed.map === 'string'
? transformed.map
: JSON.stringify(transformed.map);
Expand Down Expand Up @@ -254,6 +277,7 @@ class ScriptTransformer {
filename,
content,
instrument,
!!(options && options.mapCoverage),
);

wrappedCode = wrap(transformedSource.code);
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-runtime/src/__tests__/ScriptTransformer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ describe('ScriptTransformer', () => {

it('writes source map if preprocessor supplies it', () => {
config = Object.assign(config, {
mapCoverage: true,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});
const scriptTransformer = new ScriptTransformer(config);
Expand All @@ -237,6 +236,7 @@ describe('ScriptTransformer', () => {

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
mapCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(fs.writeFileSync).toBeCalledWith(
Expand All @@ -248,7 +248,6 @@ describe('ScriptTransformer', () => {

it('writes source map if preprocessor inlines it', () => {
config = Object.assign(config, {
mapCoverage: true,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});
const scriptTransformer = new ScriptTransformer(config);
Expand All @@ -267,6 +266,7 @@ describe('ScriptTransformer', () => {

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
mapCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(fs.writeFileSync).toBeCalledWith(
Expand All @@ -276,9 +276,8 @@ describe('ScriptTransformer', () => {
);
});

it('does not write source map if mapCoverage config option is false', () => {
it('does not write source map if mapCoverage option is false', () => {
config = Object.assign(config, {
mapCoverage: false,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});
const scriptTransformer = new ScriptTransformer(config);
Expand All @@ -295,6 +294,7 @@ describe('ScriptTransformer', () => {

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
mapCoverage: false,
});
expect(result.sourceMapPath).toBeFalsy();
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type CoverageOptions = {
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
collectCoverageOnlyFrom: ?{[key: string]: boolean},
mapCoverage: boolean,
};

type BooleanObject = {[key: string]: boolean};
Expand Down Expand Up @@ -121,6 +122,7 @@ class Runtime {
collectCoverage: false,
collectCoverageFrom: [],
collectCoverageOnlyFrom: null,
mapCoverage: false,
};
this._currentlyExecutingModulePath = '';
this._environment = environment;
Expand Down Expand Up @@ -186,6 +188,7 @@ class Runtime {
collectCoverage: options.collectCoverage,
collectCoverageFrom: options.collectCoverageFrom,
collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
mapCoverage: options.mapCoverage,
},
config,
);
Expand Down Expand Up @@ -500,6 +503,7 @@ class Runtime {
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
isInternalModule,
mapCoverage: this._coverageOptions.mapCoverage,
},
this._cacheFS[filename],
);
Expand Down

0 comments on commit 2e9da86

Please sign in to comment.