Skip to content

Commit 17f6c83

Browse files
ZacheSimenB
authored andcommittedJan 16, 2020
jest-reporters: Use global coverage thresholds as high watermarks (#9416)
1 parent 72040d9 commit 17f6c83

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
2929
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
3030
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
31+
- `[jest-reporters]` Provides global coverage thresholds as watermarks for istanbul ([#9416](https://github.com/facebook/jest/pull/9416))
3132
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
3233
- `[jest-runtime]` [**BREAKING**] Do not export `ScriptTransformer` - it can be imported from `@jest/transform` instead ([#9256](https://github.com/facebook/jest/pull/9256))
3334
- `[jest-runtime]` Use `JestEnvironment.compileFunction` if available to avoid the module wrapper ([#9252](https://github.com/facebook/jest/pull/9252))

‎packages/jest-reporters/src/__tests__/coverage_reporter.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
jest
99
.mock('istanbul-lib-source-maps')
1010
.mock('istanbul-lib-report', () => ({
11+
...jest.requireActual('istanbul-lib-report'),
1112
createContext: jest.fn(),
1213
summarizers: {pkg: jest.fn(() => ({visit: jest.fn()}))},
1314
}))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import getWatermarks from '../get_watermarks';
9+
import {makeGlobalConfig} from '../../../../TestUtils';
10+
11+
describe('getWatermarks', () => {
12+
test(`that watermarks use thresholds as upper target`, () => {
13+
const watermarks = getWatermarks(
14+
makeGlobalConfig({
15+
coverageThreshold: {
16+
global: {
17+
branches: 100,
18+
functions: 100,
19+
lines: 100,
20+
statements: 100,
21+
},
22+
},
23+
}),
24+
);
25+
26+
expect(watermarks).toEqual({
27+
branches: [expect.any(Number), 100],
28+
functions: [expect.any(Number), 100],
29+
lines: [expect.any(Number), 100],
30+
statements: [expect.any(Number), 100],
31+
});
32+
});
33+
34+
test(`that watermarks are created always created`, () => {
35+
const watermarks = getWatermarks(makeGlobalConfig());
36+
37+
expect(watermarks).toEqual({
38+
branches: [expect.any(Number), expect.any(Number)],
39+
functions: [expect.any(Number), expect.any(Number)],
40+
lines: [expect.any(Number), expect.any(Number)],
41+
statements: [expect.any(Number), expect.any(Number)],
42+
});
43+
});
44+
});

‎packages/jest-reporters/src/coverage_reporter.ts

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {RawSourceMap} from 'source-map';
2727
import {TransformResult} from '@jest/transform';
2828
import BaseReporter from './base_reporter';
2929
import {Context, CoverageReporterOptions, CoverageWorker, Test} from './types';
30+
import getWatermarks from './get_watermarks';
3031

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

499501
return {map, reportContext};
@@ -506,6 +508,7 @@ export default class CoverageReporter extends BaseReporter {
506508
dir: this._globalConfig.coverageDirectory,
507509
// @ts-ignore
508510
sourceFinder: this._sourceMapStore.sourceFinder,
511+
watermarks: getWatermarks(this._globalConfig),
509512
});
510513

511514
// @ts-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {Config} from '@jest/types';
9+
import istanbulReport = require('istanbul-lib-report');
10+
11+
export default function getWatermarks(
12+
config: Config.GlobalConfig,
13+
): istanbulReport.Watermarks {
14+
const defaultWatermarks = istanbulReport.getDefaultWatermarks();
15+
16+
const {coverageThreshold} = config;
17+
18+
if (!coverageThreshold || !coverageThreshold.global) {
19+
return defaultWatermarks;
20+
}
21+
22+
const keys: Array<keyof Config.CoverageThresholdValue> = [
23+
'branches',
24+
'functions',
25+
'lines',
26+
'statements',
27+
];
28+
return keys.reduce((watermarks, key) => {
29+
const value = coverageThreshold.global[key];
30+
if (value !== undefined) {
31+
watermarks[key][1] = value;
32+
}
33+
34+
return watermarks;
35+
}, defaultWatermarks);
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.