Skip to content

Commit

Permalink
coverage collection (jestjs#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored and cpojer committed Aug 2, 2016
1 parent 6760c6c commit db6fbd9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 69 deletions.
34 changes: 13 additions & 21 deletions integration_tests/__tests__/__snapshots__/transform-test.js.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
exports[`babel-jest instruments only specific files and collects coverage 1`] = `
"Using <<REPLACED>>, jasmine2, babel-jest
----------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------------------------|----------|----------|----------|----------|----------------|
this-directory-is-covered/ | 83.33 | 100 | 50 | 83.33 | |
covered.js | 83.33 | 100 | 50 | 83.33 | 18 |
----------------------------|----------|----------|----------|----------|----------------|
All files | 83.33 | 100 | 50 | 83.33 | |
----------------------------|----------|----------|----------|----------|----------------|
------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
------------|----------|----------|----------|----------|----------------|
All files | 83.33 | 100 | 50 | 83.33 | |
covered.js | 83.33 | 100 | 50 | 83.33 | 18 |
------------|----------|----------|----------|----------|----------------|
"
`;
Expand All @@ -17,23 +14,18 @@ exports[`custom preprocessor instruments files 1`] = `
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|----------------|
"
`;
exports[`no babel-jest instrumentation with no babel-jest 1`] = `
"Using <<REPLACED>>, jasmine2
----------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------------------------|----------|----------|----------|----------|----------------|
this-directory-is-covered/ | 83.33 | 100 | 50 | 83.33 | |
covered.js | 83.33 | 100 | 50 | 83.33 | 18 |
----------------------------|----------|----------|----------|----------|----------------|
All files | 83.33 | 100 | 50 | 83.33 | |
----------------------------|----------|----------|----------|----------|----------------|
------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
------------|----------|----------|----------|----------|----------------|
All files | 83.33 | 100 | 50 | 83.33 | |
covered.js | 83.33 | 100 | 50 | 83.33 | 18 |
------------|----------|----------|----------|----------|----------------|
"
`;
2 changes: 1 addition & 1 deletion integration_tests/__tests__/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('custom preprocessor', () => {

it('instruments files', () => {
const {stdout, status} = runJest(dir, ['--no-cache', '--coverage']);
// coverage should be empty (100%) because there's no real instrumentation
// coverage should be empty because there's no real instrumentation
expect(stripJestVersion(stdout)).toMatchSnapshot();
expect(status).toBe(0);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"chalk": "^1.1.1",
"graceful-fs": "^4.1.3",
"istanbul": "^0.4.4",
"istanbul-api": "^1.0.0-aplha.10",
"istanbul-lib-coverage": "^1.0.0-alpha.4",
"jest-changed-files": "^14.0.0",
"jest-config": "^14.1.0",
"jest-environment-jsdom": "^14.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const Test = require('./Test');

const fs = require('graceful-fs');
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
const CoverageReporter = require('./reporters/CoverageReporter');
const DefaultReporter = require('./reporters/DefaultReporter');
const NotifyReporter = require('./reporters/NotifyReporter');
const SummaryReporter = require('./reporters/SummaryReporter');
Expand Down Expand Up @@ -266,6 +265,7 @@ class TestRunner {
}

if (this._config.collectCoverage) {
const CoverageReporter = require('./reporters/CoverageReporter');
this.addReporter(new CoverageReporter());
}
}
Expand Down
44 changes: 25 additions & 19 deletions packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
import type {Config} from 'types/Config';
import type {AggregatedResult, TestResult} from 'types/TestResult';

type CoverageMap = {
merge: (data: Object) => void,
getCoverageSummary: () => Object,
};

const BaseReporter = require('./BaseReporter');

const {createReporter} = require('istanbul-api');
const chalk = require('chalk');
const istanbul = require('istanbul');
const istanbulCoverage = require('istanbul-lib-coverage');

const FAIL_COLOR = chalk.bold.red;

class CoverageReporter extends BaseReporter {
_collector: istanbul.Collector;
_testCollectors: Object;
_coverageMap: CoverageMap;

constructor() {
super();
this._collector = new istanbul.Collector();
this._testCollectors = Object.create(null);
this._coverageMap = istanbulCoverage.createCoverageMap({});
}

onTestResult(
Expand All @@ -35,27 +39,28 @@ class CoverageReporter extends BaseReporter {
aggregatedResults: AggregatedResult,
) {
if (testResult.coverage) {
const testFilePath = testResult.testFilePath;
this._collector.add(testResult.coverage);
if (!this._testCollectors[testFilePath]) {
this._testCollectors[testFilePath] = new istanbul.Collector();
}
this._testCollectors[testFilePath].add(testResult.coverage);
this._coverageMap.merge(testResult.coverage);
}
}

onRunComplete(config: Config, aggregatedResults: AggregatedResult) {
const reporter = new istanbul.Reporter();
const reporter = createReporter();
try {
if (config.coverageDirectory) {
reporter.dir = config.coverageDirectory;
}
reporter.addAll(config.coverageReporters);
reporter.write(this._collector, true, () => {});
} catch (e) {}
reporter.addAll(config.coverageReporters || []);
reporter.write(this._coverageMap);
} catch (e) {
console.error(chalk.red(`
Failed to write coverage reports:
ERROR: ${e.toString()}
STACK: ${e.stack}
`));
}

if (config.coverageThreshold) {
const rawCoverage = this._collector.getFinalCoverage();
const globalResults = istanbul.utils.summarizeCoverage(rawCoverage);
const globalResults = this._coverageMap.getCoverageSummary().toJSON();

function check(name, thresholds, actuals) {
return [
Expand Down Expand Up @@ -99,8 +104,9 @@ class CoverageReporter extends BaseReporter {
}
}

getTestCollectors() {
return this._testCollectors;
// Only exposed for the internal runner. Should not be used
getCoverageMap(): CoverageMap {
return this._coverageMap;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,28 @@
'use strict';

jest.disableAutomock().mock('fs');
jest.mock('istanbul', () => ({
Collector: jest.fn(() => ({
getFinalCoverage: jest.fn(),
})),
Reporter: jest.fn(),
utils: {
summarizeCoverage: jest.fn(),
},
}));

let istanbul;
jest.mock('istanbul-lib-coverage');
jest.mock('istanbul-api');

let libCoverage;
let CoverageReporter;
let istanbulApi;

beforeEach(() => {
istanbul = require('istanbul');
istanbulApi = require('istanbul-api');
istanbulApi.createReporter = jest.fn(() => ({
addAll: jest.fn(),
write: jest.fn(),
}));

CoverageReporter = require('../CoverageReporter');
libCoverage = require('istanbul-lib-coverage');
});

describe('onRunComplete', () => {
let mockAggResults;
let testReporter;
const globalResults = {
statements: {
pct: 50,
},
branches: {
pct: 0,
},
lines: {
pct: 0,
},
functions: {
pct: 0,
},
};

beforeEach(() => {
mockAggResults = {
Expand All @@ -62,9 +49,25 @@ describe('onRunComplete', () => {
testFilePath: 'foo',
};

libCoverage.createCoverageMap = jest.fn(() => {
return {
getCoverageSummary() {
return {
toJSON() {
return {
branches: {total: 0, covered: 0, skipped: 0, pct: 0},
functions: {total: 0, covered: 0, skipped: 0, pct: 0},
lines: {total: 0, covered: 0, skipped: 0, pct: 0},
statements: {total: 0, covered: 0, skipped: 0, pct: 50},
};
},
};
},
};
});

testReporter = new CoverageReporter();
testReporter.log = jest.fn();
istanbul.utils.summarizeCoverage.mockReturnValue(globalResults);
});

it('getLastError() returns an error when threshold is not met', () => {
Expand Down

0 comments on commit db6fbd9

Please sign in to comment.