Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-jest-plugin",
"comment": "Add a Jest threshold evaluator for combined Istanbul coverage maps as a foundation for shard aggregation.",
"type": "minor"
}
],
"packageName": "@rushstack/heft-jest-plugin"
}
8 changes: 8 additions & 0 deletions common/config/rush/browser-approved-packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"name": "@fluentui/react-components",
"allowedCategories": [ "vscode-extensions" ]
},
{
"name": "@jest/test-result",
"allowedCategories": [ "libraries" ]
},
{
"name": "@jridgewell/sourcemap-codec",
"allowedCategories": [ "libraries" ]
Expand Down Expand Up @@ -90,6 +94,10 @@
"name": "dependency-path",
"allowedCategories": [ "libraries" ]
},
{
"name": "istanbul-lib-coverage",
"allowedCategories": [ "libraries" ]
},
{
"name": "office-ui-fabric-core",
"allowedCategories": [ "libraries" ]
Expand Down
7 changes: 7 additions & 0 deletions common/config/rush/pnpm-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@
}
},

"jest-validate@30.3.0": {
"dependencies": {
// The public declarations import yargs, but its typings are only a devDependency.
"@types/yargs": "17.0.35"
}
},

"query-ast": {
"dependencies": {
"lodash": "~4.17.15"
Expand Down
15 changes: 9 additions & 6 deletions common/config/subspaces/build-tests-subspace/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "18a657d83020df41f6602b7c9087b7253cfee8e0",
"pnpmShrinkwrapHash": "f63f9d281b356fee2506f50e94415c4f13012d3a",
"preferredVersionsHash": "550b4cee0bef4e97db6c6aad726df5149d20e7d9",
"packageJsonInjectedDependenciesHash": "293debc6ed16567f5567a7748229a73be6647cc6"
"packageJsonInjectedDependenciesHash": "bde41adfdf94e369cd59f5b7f2dd4c200cbb3021"
}
12 changes: 11 additions & 1 deletion common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/subspaces/default/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "a21e9138c38033203ce379c3ec1a9aa12562d3c6",
"pnpmShrinkwrapHash": "0d76de3b212de10052098c0b74d2ebb11e3973dd",
"preferredVersionsHash": "029c99bd6e65c5e1f25e2848340509811ff9753c"
}
3 changes: 3 additions & 0 deletions heft-plugins/heft-jest-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This is a Heft plugin for running Jest.

The coverage-threshold helper evaluates a combined Istanbul map using Jest's reporter lifecycle.
It is a foundation for checking coverage once after shard aggregation; it does not enable sharding.

## Links

- [CHANGELOG.md](
Expand Down
3 changes: 3 additions & 0 deletions heft-plugins/heft-jest-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@
"dependencies": {
"@jest/core": "~30.3.0",
"@jest/reporters": "~30.3.0",
"@jest/test-result": "~30.3.0",
"@jest/transform": "~30.3.0",
"@rushstack/heft-config-file": "workspace:*",
"@rushstack/node-core-library": "workspace:*",
"@rushstack/terminal": "workspace:*",
"istanbul-lib-coverage": "3.2.2",
"jest-config": "~30.3.0",
"jest-resolve": "~30.3.0",
"jest-snapshot": "~30.3.0"
},
"devDependencies": {
"@jest/types": "30.3.0",
"@rushstack/heft": "workspace:*",
"@types/istanbul-lib-coverage": "2.0.6",
"@types/node": "20.17.19",
"decoupled-local-node-rig": "workspace:*",
"eslint": "~9.37.0",
Expand Down
94 changes: 94 additions & 0 deletions heft-plugins/heft-jest-plugin/src/JestCoverageThresholds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import * as path from 'node:path';

import type { AggregatedResult, BaseReporter, Config } from '@jest/reporters';
import { createEmptyTestResult, makeEmptyAggregatedTestResult } from '@jest/test-result';
import { readConfigs } from 'jest-config';
import type { CoverageMap } from 'istanbul-lib-coverage';

/**
* Evaluates Jest thresholds against an already combined Istanbul map, without collecting coverage or writing reports.
* Map paths must be absolute; threshold scopes are resolved relative to the build folder.
* Jest logs threshold failures and the returned messages use build-folder-relative, slash-normalized paths.
* Scopes that resolve to the same key are rejected rather than silently dropping a threshold.
*/
export async function evaluateThresholdsAsync(
buildFolderPath: string,
thresholds: Config.GlobalConfig['coverageThreshold'],
map: CoverageMap
): Promise<string[]> {
if (!thresholds) {
return [];
}

const rootDir: string = path.resolve(buildFolderPath);
const { CoverageReporter } = await import('@jest/reporters');
const absoluteThresholds: NonNullable<Config.GlobalConfig['coverageThreshold']> = Object.create(null);
const originalScopes: Map<string, string> = new Map();
for (const [name, threshold] of Object.entries(thresholds)) {
const resolvedName: string = path.resolve(rootDir, name);
const suffix: string =
(name.endsWith('/') || name.endsWith(path.sep)) && !resolvedName.endsWith(path.sep) ? path.sep : '';
const absoluteName: string = name === 'global' ? name : `${resolvedName}${suffix}`;
const previousScope: string | undefined = originalScopes.get(absoluteName);
if (previousScope !== undefined) {
throw new Error(
`Jest coverage threshold scopes "${previousScope}" and "${name}" resolve to the same path.`
);
}
originalScopes.set(absoluteName, name);
absoluteThresholds[absoluteName] = threshold;
}

const { globalConfig } = await readConfigs(
{
$0: 'heft-jest-coverage-thresholds',
_: [],
config: JSON.stringify({
rootDir,
coverageThreshold: absoluteThresholds,
coverageReporters: []
}),
// Inputs are already Istanbul maps, even when originally collected by V8.
coverageProvider: 'babel',
useStderr: true,
runInBand: true
},
[rootDir]
);
// BaseReporter's public lifecycle permits an absent Test; CoverageReporter only consumes the result.
const reporter: BaseReporter = new CoverageReporter(globalConfig, {
firstRun: true,
previousSuccess: true
});
const results: AggregatedResult = makeEmptyAggregatedTestResult();
reporter.onTestResult(undefined, { ...createEmptyTestResult(), coverage: map.toJSON() });
// No test contexts means no untested files can be recollected.
await reporter.onRunComplete(new Set(), results);
if (!results.coverageMap) {
throw new Error('Jest coverage threshold evaluation did not complete.');
}

const normalizedRoot: string = rootDir.replaceAll('\\', '/');
const rootPrefix: string = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`;
return (
reporter
.getLastError()
?.message.split('\n')
.map((message) => {
let normalizedMessage: string = message.replaceAll('\\', '/');
for (const rootScope of [normalizedRoot, rootPrefix]) {
normalizedMessage = normalizedMessage
.replaceAll(`"${rootScope}"`, '"."')
.replaceAll(`exceeds ${rootScope} threshold`, 'exceeds . threshold')
.replaceAll(`for ${rootScope} was not found.`, 'for . was not found.');
}
return normalizedMessage
.replaceAll(`"${rootPrefix}`, '"')
.replaceAll(`exceeds ${rootPrefix}`, 'exceeds ')
.replaceAll(`for ${rootPrefix}`, 'for ');
}) ?? []
);
}
Loading
Loading