From b1738e7f31efc396b7ea96fd33af2456c0bcd855 Mon Sep 17 00:00:00 2001 From: yashwanth195 <59129034+yashwanth195@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:03:38 -0800 Subject: [PATCH] Evaluate aggregated coverage thresholds --- .../jest-coverage-thresholds_2026-11-13.json | 10 + .../rush/browser-approved-packages.json | 8 + common/config/rush/pnpm-config.json | 7 + .../build-tests-subspace/pnpm-lock.yaml | 15 +- .../build-tests-subspace/repo-state.json | 4 +- .../config/subspaces/default/pnpm-lock.yaml | 12 +- .../config/subspaces/default/repo-state.json | 2 +- heft-plugins/heft-jest-plugin/README.md | 3 + heft-plugins/heft-jest-plugin/package.json | 3 + .../src/JestCoverageThresholds.ts | 94 +++++ .../src/test/JestCoverageThresholds.test.ts | 370 ++++++++++++++++++ 11 files changed, 518 insertions(+), 10 deletions(-) create mode 100644 common/changes/@rushstack/heft-jest-plugin/jest-coverage-thresholds_2026-11-13.json create mode 100644 heft-plugins/heft-jest-plugin/src/JestCoverageThresholds.ts create mode 100644 heft-plugins/heft-jest-plugin/src/test/JestCoverageThresholds.test.ts diff --git a/common/changes/@rushstack/heft-jest-plugin/jest-coverage-thresholds_2026-11-13.json b/common/changes/@rushstack/heft-jest-plugin/jest-coverage-thresholds_2026-11-13.json new file mode 100644 index 00000000000..aec14057b33 --- /dev/null +++ b/common/changes/@rushstack/heft-jest-plugin/jest-coverage-thresholds_2026-11-13.json @@ -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" +} diff --git a/common/config/rush/browser-approved-packages.json b/common/config/rush/browser-approved-packages.json index 9420e1c3669..d4d047458c4 100644 --- a/common/config/rush/browser-approved-packages.json +++ b/common/config/rush/browser-approved-packages.json @@ -10,6 +10,10 @@ "name": "@fluentui/react-components", "allowedCategories": [ "vscode-extensions" ] }, + { + "name": "@jest/test-result", + "allowedCategories": [ "libraries" ] + }, { "name": "@jridgewell/sourcemap-codec", "allowedCategories": [ "libraries" ] @@ -90,6 +94,10 @@ "name": "dependency-path", "allowedCategories": [ "libraries" ] }, + { + "name": "istanbul-lib-coverage", + "allowedCategories": [ "libraries" ] + }, { "name": "office-ui-fabric-core", "allowedCategories": [ "libraries" ] diff --git a/common/config/rush/pnpm-config.json b/common/config/rush/pnpm-config.json index 95be7286441..a649827b008 100644 --- a/common/config/rush/pnpm-config.json +++ b/common/config/rush/pnpm-config.json @@ -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" diff --git a/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml b/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml index 08f760e7c08..4c6bea0c7d7 100644 --- a/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml +++ b/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml @@ -11,7 +11,7 @@ overrides: loader-utils@^2.0.0: 2.0.4 fast-xml-parser@^5.3.3: 5.3.5 -packageExtensionsChecksum: sha256-8fXYR9X9qRA57SZJJSADz6C9KMP6QQYYut4DHyehah0= +packageExtensionsChecksum: sha256-aQCc8suEEjDkZ6212n8aDPAMEbmsh/W5ouL8vVK2nJY= pnpmfileChecksum: sha256-FceiOBivXBfnQ6+mNFF4or5E/6NvJXB3oz3cjl5O7OM= @@ -919,7 +919,7 @@ packages: resolution: {directory: ../../../heft-plugins/heft-api-extractor-plugin, type: directory} engines: {node: '>=20.9.0'} peerDependencies: - '@rushstack/heft': 1.3.4 + '@rushstack/heft': 1.3.5 '@rushstack/heft-config-file@file:../../../libraries/heft-config-file': resolution: {directory: ../../../libraries/heft-config-file, type: directory} @@ -929,7 +929,7 @@ packages: resolution: {directory: ../../../heft-plugins/heft-jest-plugin, type: directory} engines: {node: '>=20.9.0'} peerDependencies: - '@rushstack/heft': ^1.3.4 + '@rushstack/heft': ^1.3.5 '@types/jest': ^30.0.0 jest-environment-jsdom: ^30.3.0 jest-environment-node: ^30.3.0 @@ -945,18 +945,18 @@ packages: resolution: {directory: ../../../heft-plugins/heft-lint-plugin, type: directory} engines: {node: '>=20.9.0'} peerDependencies: - '@rushstack/heft': 1.3.4 + '@rushstack/heft': 1.3.5 '@rushstack/heft-node-rig@file:../../../rigs/heft-node-rig': resolution: {directory: ../../../rigs/heft-node-rig, type: directory} peerDependencies: - '@rushstack/heft': ^1.3.4 + '@rushstack/heft': ^1.3.5 '@rushstack/heft-typescript-plugin@file:../../../heft-plugins/heft-typescript-plugin': resolution: {directory: ../../../heft-plugins/heft-typescript-plugin, type: directory} engines: {node: '>=20.9.0'} peerDependencies: - '@rushstack/heft': 1.3.4 + '@rushstack/heft': 1.3.5 '@rushstack/heft@file:../../../apps/heft': resolution: {directory: ../../../apps/heft, type: directory} @@ -4829,11 +4829,13 @@ snapshots: dependencies: '@jest/core': 30.3.0 '@jest/reporters': 30.3.0 + '@jest/test-result': 30.3.0(@types/node@20.17.19) '@jest/transform': 30.3.0 '@rushstack/heft': file:../../../apps/heft(@types/node@20.17.19) '@rushstack/heft-config-file': file:../../../libraries/heft-config-file(@types/node@20.17.19) '@rushstack/node-core-library': file:../../../libraries/node-core-library(@types/node@20.17.19) '@rushstack/terminal': file:../../../libraries/terminal(@types/node@20.17.19) + istanbul-lib-coverage: 3.2.2 jest-config: 30.3.0(@types/node@20.17.19) jest-resolve: 30.3.0 jest-snapshot: 30.3.0 @@ -7109,6 +7111,7 @@ snapshots: dependencies: '@jest/get-type': 30.1.0 '@jest/types': 30.3.0 + '@types/yargs': 17.0.35 camelcase: 6.3.0 chalk: 4.1.2 leven: 3.1.0 diff --git a/common/config/subspaces/build-tests-subspace/repo-state.json b/common/config/subspaces/build-tests-subspace/repo-state.json index 1449961bcde..13dbced4af6 100644 --- a/common/config/subspaces/build-tests-subspace/repo-state.json +++ b/common/config/subspaces/build-tests-subspace/repo-state.json @@ -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" } diff --git a/common/config/subspaces/default/pnpm-lock.yaml b/common/config/subspaces/default/pnpm-lock.yaml index 13b7ce39782..1bbb04fa418 100644 --- a/common/config/subspaces/default/pnpm-lock.yaml +++ b/common/config/subspaces/default/pnpm-lock.yaml @@ -11,7 +11,7 @@ overrides: loader-utils@^2.0.0: 2.0.4 fast-xml-parser@^5.3.3: 5.3.5 -packageExtensionsChecksum: sha256-8fXYR9X9qRA57SZJJSADz6C9KMP6QQYYut4DHyehah0= +packageExtensionsChecksum: sha256-aQCc8suEEjDkZ6212n8aDPAMEbmsh/W5ouL8vVK2nJY= pnpmfileChecksum: sha256-FceiOBivXBfnQ6+mNFF4or5E/6NvJXB3oz3cjl5O7OM= @@ -3304,6 +3304,9 @@ importers: '@jest/reporters': specifier: ~30.3.0 version: 30.3.0 + '@jest/test-result': + specifier: ~30.3.0 + version: 30.3.0(@types/node@20.17.19) '@jest/transform': specifier: ~30.3.0 version: 30.3.0 @@ -3316,6 +3319,9 @@ importers: '@rushstack/terminal': specifier: workspace:* version: link:../../libraries/terminal + istanbul-lib-coverage: + specifier: 3.2.2 + version: 3.2.2 jest-config: specifier: ~30.3.0 version: 30.3.0(@types/node@20.17.19)(babel-plugin-macros@3.1.0)(esbuild-register@3.6.0(esbuild@0.28.0)) @@ -3332,6 +3338,9 @@ importers: '@rushstack/heft': specifier: workspace:* version: link:../../apps/heft + '@types/istanbul-lib-coverage': + specifier: 2.0.6 + version: 2.0.6 '@types/node': specifier: 20.17.19 version: 20.17.19 @@ -33753,6 +33762,7 @@ snapshots: dependencies: '@jest/get-type': 30.1.0 '@jest/types': 30.3.0 + '@types/yargs': 17.0.35 camelcase: 6.3.0 chalk: 4.1.2 leven: 3.1.0 diff --git a/common/config/subspaces/default/repo-state.json b/common/config/subspaces/default/repo-state.json index 3d09725be2d..5a9907bf7cd 100644 --- a/common/config/subspaces/default/repo-state.json +++ b/common/config/subspaces/default/repo-state.json @@ -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" } diff --git a/heft-plugins/heft-jest-plugin/README.md b/heft-plugins/heft-jest-plugin/README.md index 25f82e5ef62..7b9b1143b15 100644 --- a/heft-plugins/heft-jest-plugin/README.md +++ b/heft-plugins/heft-jest-plugin/README.md @@ -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]( diff --git a/heft-plugins/heft-jest-plugin/package.json b/heft-plugins/heft-jest-plugin/package.json index 53c493bc2ac..0c5788d7db6 100644 --- a/heft-plugins/heft-jest-plugin/package.json +++ b/heft-plugins/heft-jest-plugin/package.json @@ -39,10 +39,12 @@ "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" @@ -50,6 +52,7 @@ "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", diff --git a/heft-plugins/heft-jest-plugin/src/JestCoverageThresholds.ts b/heft-plugins/heft-jest-plugin/src/JestCoverageThresholds.ts new file mode 100644 index 00000000000..dd4053cee1d --- /dev/null +++ b/heft-plugins/heft-jest-plugin/src/JestCoverageThresholds.ts @@ -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 { + if (!thresholds) { + return []; + } + + const rootDir: string = path.resolve(buildFolderPath); + const { CoverageReporter } = await import('@jest/reporters'); + const absoluteThresholds: NonNullable = Object.create(null); + const originalScopes: Map = 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 '); + }) ?? [] + ); +} diff --git a/heft-plugins/heft-jest-plugin/src/test/JestCoverageThresholds.test.ts b/heft-plugins/heft-jest-plugin/src/test/JestCoverageThresholds.test.ts new file mode 100644 index 00000000000..25e194fb214 --- /dev/null +++ b/heft-plugins/heft-jest-plugin/src/test/JestCoverageThresholds.test.ts @@ -0,0 +1,370 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. +// See LICENSE in the project root for license information. + +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import { stripVTControlCharacters } from 'node:util'; + +import { runCLI } from '@jest/core'; +import { CoverageReporter, type BaseReporter, type Config } from '@jest/reporters'; +import { createEmptyTestResult, makeEmptyAggregatedTestResult } from '@jest/test-result'; +import { readConfigs } from 'jest-config'; +import { createCoverageMap, type CoverageMap, type FileCoverageData } from 'istanbul-lib-coverage'; + +import { evaluateThresholdsAsync } from '../JestCoverageThresholds'; + +function createMap(rootDir: string, filename: string, hits: [number, number]): CoverageMap { + /* eslint-disable @typescript-eslint/naming-convention -- Istanbul coverage IDs are numeric strings. */ + const data: FileCoverageData = { + path: path.join(rootDir, filename), + statementMap: { + '0': { start: { line: 1, column: 0 }, end: { line: 1, column: 10 } }, + '1': { start: { line: 2, column: 0 }, end: { line: 2, column: 10 } } + }, + fnMap: { + '0': { + name: 'first', + decl: { start: { line: 1, column: 0 }, end: { line: 1, column: 5 } }, + loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 10 } }, + line: 1 + }, + '1': { + name: 'second', + decl: { start: { line: 2, column: 0 }, end: { line: 2, column: 5 } }, + loc: { start: { line: 2, column: 0 }, end: { line: 2, column: 10 } }, + line: 2 + } + }, + branchMap: { + '0': { + type: 'if', + line: 1, + loc: { start: { line: 1, column: 0 }, end: { line: 2, column: 10 } }, + locations: [ + { start: { line: 1, column: 0 }, end: { line: 1, column: 10 } }, + { start: { line: 2, column: 0 }, end: { line: 2, column: 10 } } + ] + } + }, + s: { '0': hits[0], '1': hits[1] }, + f: { '0': hits[0], '1': hits[1] }, + b: { '0': [...hits] } + }; + /* eslint-enable @typescript-eslint/naming-convention */ + return createCoverageMap({ [data.path]: data }); +} + +describe('evaluateThresholdsAsync', () => { + let rootDir: string; + let logSpy: jest.SpyInstance; + + function normalize(message: string): string { + return stripVTControlCharacters(message) + .replaceAll('\\', '/') + .replaceAll(`${rootDir.replaceAll('\\', '/')}/`, ''); + } + + async function expectThresholdsAsync( + thresholds: Config.GlobalConfig['coverageThreshold'], + map: CoverageMap, + expected: string[], + buildFolderPath: string = rootDir + ): Promise { + logSpy.mockClear(); + const original: string = JSON.stringify(map); + expect(await evaluateThresholdsAsync(buildFolderPath, thresholds, map)).toEqual(expected); + expect(logSpy.mock.calls.map(([message]) => normalize(message))).toEqual( + expected.length ? [expected.join('\n')] : [] + ); + expect(JSON.stringify(map)).toBe(original); + } + + beforeEach(async () => { + const tempFolder: string = path.resolve(__dirname, '../../temp'); + await fs.mkdir(tempFolder, { recursive: true }); + rootDir = await fs.realpath(await fs.mkdtemp(path.join(tempFolder, 'jest-thresholds-'))); + logSpy = jest.spyOn(CoverageReporter.prototype, 'log').mockImplementation(() => {}); + }); + + afterEach(async () => { + jest.restoreAllMocks(); + await fs.rm(rootDir, { recursive: true, force: true }); + }); + + it.each(['branches', 'functions', 'lines', 'statements'] as const)( + 'preserves percentage boundaries and uncovered-count limits for %s', + async (metric) => { + const map: CoverageMap = createMap(rootDir, 'source.js', [1, 0]); + await expectThresholdsAsync({ global: { [metric]: 50 } }, map, []); + await expectThresholdsAsync({ global: { [metric]: 51 } }, map, [ + `Jest: "global" coverage threshold for ${metric} (51%) not met: 50%` + ]); + await expectThresholdsAsync({ global: { [metric]: -1 } }, map, []); + const uncovered: CoverageMap = createMap(rootDir, 'source.js', [0, 0]); + await expectThresholdsAsync({ global: { [metric]: -2 } }, uncovered, []); + await expectThresholdsAsync({ global: { [metric]: -1 } }, uncovered, [ + `Jest: Uncovered count for ${metric} (2) exceeds global threshold (1)` + ]); + } + ); + + it('skips absent thresholds and accepts disabled metrics and an empty map', async () => { + const map: CoverageMap = createMap(rootDir, 'source.js', [0, 0]); + await expectThresholdsAsync(undefined, map, [], path.join(rootDir, 'does-not-exist')); + await expectThresholdsAsync({ global: {} }, map, []); + await expectThresholdsAsync({ global: { statements: 0 } }, map, []); + await expectThresholdsAsync({ global: { statements: 100 } }, createCoverageMap({}), []); + }); + + it.each(['./src/', `.${path.sep}src${path.sep}`])( + 'resolves directory scope %s against the build folder and excludes it from global', + async (scope) => { + const map: CoverageMap = createMap(rootDir, 'src/first.js', [1, 1]); + map.merge(createMap(rootDir, 'src/second.js', [0, 0])); + map.merge(createMap(rootDir, 'src-other/third.js', [1, 1])); + await expectThresholdsAsync({ global: { statements: 100 }, [scope]: { statements: 50 } }, map, []); + await expectThresholdsAsync( + { global: { statements: 100 }, [scope]: { statements: 51 } }, + map, + ['Jest: "src/" coverage threshold for statements (51%) not met: 50%'], + `${rootDir}${path.sep}` + ); + } + ); + + it('supports absolute and root-relative file scopes, including overlapping scopes', async () => { + const map: CoverageMap = createMap(rootDir, 'src/first.js', [1, 0]); + map.merge(createMap(rootDir, 'other.js', [1, 1])); + for (const scope of ['./src/first.js', path.join(rootDir, 'src/first.js')]) { + await expectThresholdsAsync( + { global: { statements: 100 }, [scope]: { statements: 51 }, './src/': { statements: 51 } }, + map, + [ + 'Jest: "src/first.js" coverage threshold for statements (51%) not met: 50%', + 'Jest: "src/" coverage threshold for statements (51%) not met: 50%' + ] + ); + } + }); + + it('checks each glob-matched file rather than averaging the directory', async () => { + await fs.mkdir(path.join(rootDir, 'src')); + await fs.writeFile(path.join(rootDir, 'src/first.js'), ''); + await fs.writeFile(path.join(rootDir, 'src/second.js'), ''); + const map: CoverageMap = createMap(rootDir, 'src/first.js', [1, 1]); + map.merge(createMap(rootDir, 'src/second.js', [0, 0])); + await expectThresholdsAsync({ global: { statements: 100 }, './src/*.js': { statements: 50 } }, map, [ + 'Jest: "src/second.js" coverage threshold for statements (50%) not met: 0%' + ]); + }); + + it.each(['./missing.js', './missing/', './missing/*.js'])('reports a missing scope %s', async (scope) => { + await expectThresholdsAsync( + { global: {}, [scope]: { statements: 100 } }, + createMap(rootDir, 'source.js', [1, 1]), + [`Jest: Coverage data for ${scope.slice(2)} was not found.`] + ); + }); + + it('returns identical messages after relocating the build root', async () => { + const thresholds: Config.GlobalConfig['coverageThreshold'] = { + global: {}, + './src/': { statements: 100 } + }; + const first: string[] = await evaluateThresholdsAsync( + rootDir, + thresholds, + createMap(rootDir, 'src/first.js', [1, 0]) + ); + const relocated: string = path.join(rootDir, 'relocated'); + await fs.mkdir(relocated); + const second: string[] = await evaluateThresholdsAsync( + relocated, + thresholds, + createMap(relocated, 'src/first.js', [1, 0]) + ); + expect(first).toEqual(['Jest: "src/" coverage threshold for statements (100%) not met: 50%']); + expect(second).toEqual(first); + expect(logSpy.mock.calls.map(([message]) => normalize(message))).toEqual([ + first.join('\n'), + first.join('\n').replace('"src/', '"relocated/src/') + ]); + }); + + it.each([false, true])( + 'rejects aliased scopes instead of hiding native Jest failures (reverse: %s)', + async (reverse) => { + const entries: [string, { statements?: number; functions?: number }][] = [ + ['./source.js', { statements: 100 }], + ['source.js', { statements: 0, functions: 100 }] + ]; + if (reverse) { + entries.reverse(); + } + const thresholds: NonNullable = { + global: {}, + ...Object.fromEntries(entries) + }; + const map: CoverageMap = createMap(rootDir, 'source.js', [0, 0]); + const { globalConfig } = await readConfigs( + { + $0: 'jest-alias-parity', + _: [], + useStderr: true, + config: JSON.stringify({ + rootDir, + coverageReporters: [], + coverageThreshold: { + global: {}, + ...Object.fromEntries(entries.map(([name, threshold]) => [`${rootDir}/${name}`, threshold])) + } + }) + }, + [rootDir] + ); + const reporter: BaseReporter = new CoverageReporter(globalConfig, { + firstRun: true, + previousSuccess: true + }); + reporter.onTestResult(undefined, { ...createEmptyTestResult(), coverage: map.toJSON() }); + await reporter.onRunComplete(new Set(), makeEmptyAggregatedTestResult()); + const expected: string[] = [ + 'Jest: "./source.js" coverage threshold for statements (100%) not met: 0%', + 'Jest: "source.js" coverage threshold for functions (100%) not met: 0%' + ]; + if (reverse) { + expected.reverse(); + } + expect(normalize(reporter.getLastError()!.message)).toBe(expected.join('\n')); + expect(logSpy.mock.calls.map(([message]) => normalize(message))).toEqual([expected.join('\n')]); + logSpy.mockClear(); + await expect(evaluateThresholdsAsync(rootDir, thresholds, map)).rejects.toThrow( + 'resolve to the same path' + ); + expect(logSpy).not.toHaveBeenCalled(); + } + ); + + it.each(['.', './', 'absolute'])( + 'normalizes root scope %s for percentage and count failures after relocation', + async (scope) => { + const relocated: string = path.join(rootDir, 'relocated'); + await fs.mkdir(relocated); + for (const folder of [rootDir, relocated]) { + for (const threshold of [100, -1]) { + logSpy.mockClear(); + const messages: string[] = await evaluateThresholdsAsync( + folder, + { global: {}, [scope === 'absolute' ? folder : scope]: { statements: threshold } }, + createMap(folder, 'source.js', [0, 0]) + ); + expect(messages).toEqual([ + threshold === 100 + ? 'Jest: "." coverage threshold for statements (100%) not met: 0%' + : 'Jest: Uncovered count for statements (2) exceeds . threshold (1)' + ]); + const absoluteScope: string = `${folder}${scope === './' ? path.sep : ''}`.replaceAll('\\', '/'); + expect( + logSpy.mock.calls.map(([message]) => stripVTControlCharacters(message).replaceAll('\\', '/')) + ).toEqual([ + threshold === 100 + ? `Jest: "${absoluteScope}" coverage threshold for statements (100%) not met: 0%` + : `Jest: Uncovered count for statements (2) exceeds ${absoluteScope} threshold (1)` + ]); + } + } + } + ); + + it('uses the public lifecycle without recollecting sources or generating reports', async () => { + await fs.writeFile(path.join(rootDir, 'uncollected.js'), 'throw new Error("must not be collected");'); + const before: string[] = await fs.readdir(rootDir, { recursive: true }); + const onTestResultSpy = jest.spyOn(CoverageReporter.prototype, 'onTestResult'); + const onRunCompleteSpy = jest.spyOn(CoverageReporter.prototype, 'onRunComplete'); + const getLastErrorSpy = jest.spyOn(CoverageReporter.prototype, 'getLastError'); + await expectThresholdsAsync( + { global: { statements: 100 } }, + createMap(rootDir, 'not-on-disk.js', [1, 1]), + [] + ); + expect(onTestResultSpy).toHaveBeenCalledTimes(1); + expect(onRunCompleteSpy).toHaveBeenCalledTimes(1); + expect(onRunCompleteSpy.mock.calls[0][0].size).toBe(0); + expect(getLastErrorSpy).toHaveBeenCalledTimes(1); + expect(await fs.readdir(rootDir, { recursive: true })).toEqual(before); + }); + + it('matches real Jest runs and passes only after complementary coverage is combined', async () => { + await fs.writeFile( + path.join(rootDir, 'source.js'), + 'exports.left = () => 1;\nexports.right = () => 2;\n' + ); + await fs.copyFile(path.join(rootDir, 'source.js'), path.join(rootDir, 'global.js')); + const thresholds: NonNullable = { + global: { functions: 100 }, + './source.js': { functions: 100 }, + './source*.js': { functions: 100 } + }; + const maps: CoverageMap[] = []; + for (const [name, value] of [ + ['left', 1], + ['right', 2] + ] as const) { + await fs.writeFile( + path.join(rootDir, 'source.test.js'), + `test('${name}', () => { + expect(require('./source').${name}()).toBe(${value}); + expect(require('./global').${name}()).toBe(${value}); + });\n` + ); + logSpy.mockClear(); + const { results } = await runCLI( + { + $0: 'jest-threshold-parity', + _: [], + runInBand: true, + useStderr: true, + config: JSON.stringify({ + rootDir, + cache: false, + testEnvironment: require.resolve('jest-environment-node'), + testMatch: ['**/source.test.js'], + collectCoverage: true, + coverageProvider: 'babel', + coverageReporters: [], + reporters: [], + coverageThreshold: { + global: thresholds.global, + [path.join(rootDir, 'source.js')]: thresholds['./source.js'], + [path.join(rootDir, 'source*.js')]: thresholds['./source*.js'] + } + }) + }, + [rootDir] + ); + expect(results.numPassedTests).toBe(1); + expect(results.numFailedTests).toBe(0); + expect(results.coverageMap?.files().sort()).toEqual( + [path.join(rootDir, 'global.js'), path.join(rootDir, 'source.js')].sort() + ); + const expected: string[] = [ + 'Jest: "global" coverage threshold for functions (100%) not met: 50%', + 'Jest: "source.js" coverage threshold for functions (100%) not met: 50%', + 'Jest: "source.js" coverage threshold for functions (100%) not met: 50%' + ]; + expect(logSpy.mock.calls.map(([message]) => normalize(message))).toEqual([expected.join('\n')]); + if (!results.coverageMap) { + throw new Error('Jest did not return a coverage map.'); + } + const map: CoverageMap = createCoverageMap(results.coverageMap.toJSON()); + maps.push(map); + await expectThresholdsAsync(thresholds, map, expected); + } + const combined: CoverageMap = createCoverageMap({}); + for (const map of maps) { + combined.merge(map); + } + expect(combined.getCoverageSummary().functions.pct).toBe(100); + await expectThresholdsAsync(thresholds, combined, []); + }); +});