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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -41,6 +41,7 @@
- `[jest-mock]` Add `contexts` member to mock functions ([#12601](https://github.com/facebook/jest/pull/12601))
- `[@jest/reporters]` Add GitHub Actions reporter ([#11320](https://github.com/facebook/jest/pull/11320), [#12658](https://github.com/facebook/jest/pull/12658))
- `[@jest/reporters]` Pass `reporterContext` to custom reporter constructors as third argument ([#12657](https://github.com/facebook/jest/pull/12657))
- `[@jest/reporters]` Fix trailing slash in matching `coverageThreshold` key ([#12714](https://github.com/facebook/jest/pull/12714))
- `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373))
- `[jest-resolve]` Support package self-reference ([#12682](https://github.com/facebook/jest/pull/12682))
- `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392))
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-reporters/src/CoverageReporter.ts
Expand Up @@ -269,7 +269,10 @@ 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 suffix = thresholdGroup.length > 1 && thresholdGroup.endsWith(path.sep) ? path.sep : '';
const absoluteThresholdGroup = `${path.resolve(thresholdGroup)}${suffix}`;

// The threshold group might be a path:

Expand Down
14 changes: 13 additions & 1 deletion packages/jest-reporters/src/__tests__/CoverageReporter.test.js
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 @@ -306,6 +312,9 @@ describe('onRunComplete', () => {
{
collectCoverage: true,
coverageThreshold: {
'./path-test/': {
statements: 100,
},
'./path-test-files/': {
statements: 50,
},
Expand Down Expand Up @@ -367,6 +376,9 @@ describe('onRunComplete', () => {
{
collectCoverage: true,
coverageThreshold: {
'./path-test/100pc_coverage_file.js': {
statements: 100,
},
'./path-test-files/100pc_coverage_file.js': {
statements: 100,
},
Expand Down