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

fix: issue with trailing slash in matching coverageThreshold keys #12714

Merged
merged 10 commits into from
Apr 27, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[@jest/reporters]` Fix trailing slash in matching `coverageThreshold` key ([#12714](https://github.com/facebook/jest/pull/12714))

### Chore & Maintenance

### Performance
Expand Down
11 changes: 10 additions & 1 deletion packages/jest-reporters/src/CoverageReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,16 @@ export default class CoverageReporter extends BaseReporter {
const pathOrGlobMatches = thresholdGroups.reduce<
Array<[string, string]>
>((agg, thresholdGroup) => {
const absoluteThresholdGroup = path.resolve(thresholdGroup);
// Preserve trailing slash, but not required if root dir
// See https://github.com/facebook/jest/issues/12703
const resolvedThresholdGroup = path.resolve(thresholdGroup);
const suffix =
(thresholdGroup.endsWith(path.sep) ||
(process.platform === 'win32' && thresholdGroup.endsWith('/'))) &&
SimenB marked this conversation as resolved.
Show resolved Hide resolved
!resolvedThresholdGroup.endsWith(path.sep)
? path.sep
: '';
const absoluteThresholdGroup = `${resolvedThresholdGroup}${suffix}`;

// The threshold group might be a path:

Expand Down
14 changes: 13 additions & 1 deletion packages/jest-reporters/src/__tests__/CoverageReporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ beforeEach(() => {
'non_covered_file.js': '',
'relative_path_file.js': '',
};

fileTree[`${process.cwd()}/path-test`] = {
'100pc_coverage_file.js': '',
};
mock(fileTree);
});

Expand Down Expand Up @@ -79,6 +81,10 @@ describe('onRunComplete', () => {
statements: {covered: 5, pct: 50, skipped: 0, total: 10},
};
const fileCoverage = [
[
'./path-test/100pc_coverage_file.js',
{statements: {covered: 10, pct: 100, total: 10}},
],
['./path-test-files/covered_file_without_threshold.js'],
['./path-test-files/full_path_file.js'],
['./path-test-files/relative_path_file.js'],
Expand Down Expand Up @@ -309,6 +315,9 @@ describe('onRunComplete', () => {
'./path-test-files/': {
statements: 50,
},
'./path-test/': {
statements: 100,
},
global: {
statements: 100,
},
Expand Down Expand Up @@ -370,6 +379,9 @@ describe('onRunComplete', () => {
'./path-test-files/100pc_coverage_file.js': {
statements: 100,
},
'./path-test/100pc_coverage_file.js': {
statements: 100,
},
global: {
statements: 50,
},
Expand Down