Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-reporters: Use global coverage thresholds as high watermarks #9416

Merged
merged 7 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
- `[jest-reporters]` Provides global coverage thresholds as watermarks for istanbul ([#9416](https://github.com/facebook/jest/pull/9416))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
- `[jest-runtime]` [**BREAKING**] Do not export `ScriptTransformer` - it can be imported from `@jest/transform` instead ([#9256](https://github.com/facebook/jest/pull/9256))
- `[jest-runtime]` Use `JestEnvironment.compileFunction` if available to avoid the module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
jest
.mock('istanbul-lib-source-maps')
.mock('istanbul-lib-report', () => ({
...jest.requireActual('istanbul-lib-report'),
createContext: jest.fn(),
summarizers: {pkg: jest.fn(() => ({visit: jest.fn()}))},
}))
Expand Down
44 changes: 44 additions & 0 deletions packages/jest-reporters/src/__tests__/get_watermarks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import getWatermarks from '../get_watermarks';
import {makeGlobalConfig} from '../../../../TestUtils';

describe('getWatermarks', () => {
test(`that watermarks use thresholds as upper target`, () => {
const watermarks = getWatermarks(
makeGlobalConfig({
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
}),
);

expect(watermarks).toEqual({
branches: [expect.any(Number), 100],
functions: [expect.any(Number), 100],
lines: [expect.any(Number), 100],
statements: [expect.any(Number), 100],
});
});

test(`that watermarks are created always created`, () => {
const watermarks = getWatermarks(makeGlobalConfig());

expect(watermarks).toEqual({
branches: [expect.any(Number), expect.any(Number)],
functions: [expect.any(Number), expect.any(Number)],
lines: [expect.any(Number), expect.any(Number)],
statements: [expect.any(Number), expect.any(Number)],
});
});
});
3 changes: 3 additions & 0 deletions packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {RawSourceMap} from 'source-map';
import {TransformResult} from '@jest/transform';
import BaseReporter from './base_reporter';
import {Context, CoverageReporterOptions, CoverageWorker, Test} from './types';
import getWatermarks from './get_watermarks';

// This is fixed in a newer versions of source-map, but our dependencies are still stuck on old versions
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
Expand Down Expand Up @@ -494,6 +495,7 @@ export default class CoverageReporter extends BaseReporter {
// @ts-ignore
coverageMap: map,
dir: this._globalConfig.coverageDirectory,
watermarks: getWatermarks(this._globalConfig),
});

return {map, reportContext};
Expand All @@ -506,6 +508,7 @@ export default class CoverageReporter extends BaseReporter {
dir: this._globalConfig.coverageDirectory,
// @ts-ignore
sourceFinder: this._sourceMapStore.sourceFinder,
watermarks: getWatermarks(this._globalConfig),
});

// @ts-ignore
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-reporters/src/get_watermarks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {Config} from '@jest/types';
import istanbulReport = require('istanbul-lib-report');

export default function getWatermarks(
config: Config.GlobalConfig,
): istanbulReport.Watermarks {
const defaultWatermarks = istanbulReport.getDefaultWatermarks();

const {coverageThreshold} = config;

if (!coverageThreshold || !coverageThreshold.global) {
return defaultWatermarks;
}

const keys: Array<keyof Config.CoverageThresholdValue> = [
'branches',
'functions',
'lines',
'statements',
];
return keys.reduce((watermarks, key) => {
const value = coverageThreshold.global[key];
SimenB marked this conversation as resolved.
Show resolved Hide resolved
if (value !== undefined) {
watermarks[key][1] = value;
}

return watermarks;
}, defaultWatermarks);
}