Skip to content
Merged
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
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ For the Apex side of step 5, see [Alongside Apex coverage](#alongside-apex-cover
## How coverage is measured

- **What counts.** Active Flows with the process types Salesforce counts toward deploy-as-active: autolaunched Flows (including record-triggered, platform event and schedule-triggered Flows) and Process Builder processes. Screen Flows don't count.
- **How Salesforce calculates it.** The percentage counts Flows, not elements: a Flow counts once any Apex test runs it. Salesforce divides the Flow versions with coverage (active ones, and the latest inactive versions that have a test) by the active versions without coverage plus the versions with coverage ([Deploy Processes and Flows as Active](https://help.salesforce.com/s/articleView?id=platform.flow_distribute_deploy_active.htm&type=5)). `readiness` calculates it the same way, and shows the active-only figure too when they differ. Element coverage is reported to show what's still untested, but it doesn't count toward the threshold.
- **Which version a test runs.** Apex tests run a Flow's active version, or its latest version when none is active, so a Flow deployed inactive gets coverage on that inactive version. Saving changes to a Flow version deletes its coverage, so run the tests again after editing a Flow ([FlowTestCoverage](https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_flowtestcoverage.htm)).
- **The threshold.** Production sets the percentage in Setup > Process Automation Settings, next to "Deploy processes and flows as active". `readiness` uses 75% unless you pass `--threshold`, and says so.
- **Where it comes from.** Salesforce records which Flow versions each Apex test runs in the Tooling API `FlowTestCoverage` object when Apex tests run in the org. `readiness`, `gate`, and `transform` read it, so run your Apex tests before using them.
- **Run tests with `--code-coverage`.** Without `--code-coverage`, `sf apex run test` skips coverage and Salesforce records no Flow coverage. Check-only deployments (`sf project deploy validate`) don't record it either. When no Flow in the org has any coverage, `readiness`, `gate`, and `transform` warn that this is the likely cause instead of just reporting 0%.
- **Only Apex counts.** Flow Builder tests and Flow Test Mode (Winter '27) don't count toward the 75% requirement, so `scaffold` generates Apex tests, not Flow test metadata. The coverage source is pluggable, so if Salesforce starts counting Test Mode coverage it can be added without changing the commands.
- **Only Apex counts.** Flow Builder tests and Flow Test Mode (Winter '27) don't count toward the requirement, so `scaffold` generates Apex tests, not Flow test metadata. The coverage source is pluggable, so if Salesforce starts counting Test Mode coverage it can be added without changing the commands.

## Readiness report

Expand Down Expand Up @@ -305,7 +308,7 @@ FLAGS
<options: human|markdown>
--since=<value> Only count commits after this date: a date (2026-01-31) or "<n> days|weeks|months|years
ago".
--threshold=<value> [default: 75] Coverage percentage the org must reach.
--threshold=<value> Coverage percentage the org must reach. Default: 75.

GLOBAL FLAGS
--flags-dir=<value> Import flag values from a directory.
Expand All @@ -314,12 +317,15 @@ GLOBAL FLAGS
DESCRIPTION
Report how close the org is to the coverage needed to deploy Flows as active.

Salesforce requires 75% of active autolaunched Flows and processes to be run by at least one Apex test before you can
turn on "Deploy processes and flows as active". This command reads FlowTestCoverage from the target org, reports the
org-wide percentage against the threshold, and lists the uncovered Flows (ranked by how often they change in git) and,
for each covered Flow, the Apex tests that run it and the elements no test runs yet. Listing those elements takes one
Tooling API query per partly covered Flow. Process Builder processes are labeled and counted, with a suggestion to
migrate them to Flows, since Salesforce no longer supports Process Builder.
Before you can turn on "Deploy processes and flows as active", Salesforce requires a percentage of active autolaunched
Flows and processes to be run by at least one Apex test. Production sets that percentage in Process Automation
Settings; this command uses 75% unless you pass --threshold. A Flow counts once any test runs it, however many of its
elements run. Salesforce also counts the latest inactive versions that have a test, and this command does the same. It
reads FlowTestCoverage from the target org, reports the org-wide percentage against the threshold, and lists the
uncovered Flows (ranked by how often they change in git) and, for each covered Flow, the Apex tests that run it and
the elements no test runs yet. Listing those elements takes one Tooling API query per partly covered Flow. Process
Builder processes are labeled and counted, with a suggestion to migrate them to Flows, since Salesforce no longer
supports Process Builder.

FlowTestCoverage is filled in when Apex tests run in the org. Run your Apex tests first with coverage turned on (for
example, `sf apex run test --test-level RunLocalTests --code-coverage --wait 60`) so the report is current. Without
Expand Down Expand Up @@ -361,6 +367,11 @@ FLAG DESCRIPTIONS
"human" prints tables and warnings to the terminal. "markdown" prints the report as one Markdown document, warnings
included, for a pull request comment or a GitHub Actions job summary. Redirect it to a file, for example `>>
"$GITHUB_STEP_SUMMARY"`. --json takes precedence over both.

--threshold=<value> Coverage percentage the org must reach. Default: 75.

Production sets the percentage Salesforce requires in Setup > Process Automation Settings, next to "Deploy processes
and flows as active". Pass that number; without it, the report uses 75% and says so.
```

_See code: [src/commands/flowcov/readiness.ts](https://github.com/mcarvin8/sf-flow-coverage/blob/main/src/commands/flowcov/readiness.ts)_
Expand Down
8 changes: 6 additions & 2 deletions messages/flowcov.readiness.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Report how close the org is to the coverage needed to deploy Flows as active.

# description

Salesforce requires 75% of active autolaunched Flows and processes to be run by at least one Apex test before you can turn on "Deploy processes and flows as active". This command reads FlowTestCoverage from the target org, reports the org-wide percentage against the threshold, and lists the uncovered Flows (ranked by how often they change in git) and, for each covered Flow, the Apex tests that run it and the elements no test runs yet. Listing those elements takes one Tooling API query per partly covered Flow. Process Builder processes are labeled and counted, with a suggestion to migrate them to Flows, since Salesforce no longer supports Process Builder.
Before you can turn on "Deploy processes and flows as active", Salesforce requires a percentage of active autolaunched Flows and processes to be run by at least one Apex test. Production sets that percentage in Process Automation Settings; this command uses 75% unless you pass --threshold. A Flow counts once any test runs it, however many of its elements run. Salesforce also counts the latest inactive versions that have a test, and this command does the same. It reads FlowTestCoverage from the target org, reports the org-wide percentage against the threshold, and lists the uncovered Flows (ranked by how often they change in git) and, for each covered Flow, the Apex tests that run it and the elements no test runs yet. Listing those elements takes one Tooling API query per partly covered Flow. Process Builder processes are labeled and counted, with a suggestion to migrate them to Flows, since Salesforce no longer supports Process Builder.

FlowTestCoverage is filled in when Apex tests run in the org. Run your Apex tests first with coverage turned on (for example, `sf apex run test --test-level RunLocalTests --code-coverage --wait 60`) so the report is current. Without `--code-coverage`, Salesforce records no Flow coverage. When no Flow in the org has any coverage recorded, the command warns that the tests most likely ran without `--code-coverage`.

Expand Down Expand Up @@ -46,7 +46,11 @@ Only count commits after this date: a date (2026-01-31) or "<n> days|weeks|month

# flags.threshold.summary

Coverage percentage the org must reach.
Coverage percentage the org must reach. Default: 75.

# flags.threshold.description

Production sets the percentage Salesforce requires in Setup > Process Automation Settings, next to "Deploy processes and flows as active". Pass that number; without it, the report uses 75% and says so.

# flags.fail-if-not-ready.summary

Expand Down
11 changes: 5 additions & 6 deletions src/commands/flowcov/readiness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Messages } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { describeElements } from '../../core/elementCoverage.js';
import { deployAsActiveAdvice } from '../../core/flowSettings.js';
import { DEFAULT_THRESHOLD, isProcessBuilder, PROCESS_BUILDER_ADVICE, processTypeLabel } from '../../core/flowTypes.js';
import { isProcessBuilder, PROCESS_BUILDER_ADVICE, processTypeLabel } from '../../core/flowTypes.js';
import { getFlowChurn } from '../../core/git.js';
import { OUTPUT_FORMATS, readinessMarkdown } from '../../core/markdown.js';
import { OrgCoverageSource } from '../../core/orgCoverageSource.js';
import { buildReadinessReport } from '../../core/readiness.js';
import { buildReadinessReport, coverageLines, ELEMENT_COVERAGE_NOTE } from '../../core/readiness.js';
import { dataQuery, metadataRead, toolingQuery } from '../../core/toolingQuery.js';
import type { ReadinessResult } from '../../core/types.js';

Expand All @@ -32,7 +32,7 @@ export default class FlowcovReadiness extends SfCommand<ReadinessResult> {
}),
threshold: Flags.integer({
summary: messages.getMessage('flags.threshold.summary'),
default: DEFAULT_THRESHOLD,
description: messages.getMessage('flags.threshold.description'),
min: 0,
max: 100,
}),
Expand Down Expand Up @@ -74,9 +74,7 @@ export default class FlowcovReadiness extends SfCommand<ReadinessResult> {
}

private logReport(result: ReadinessResult): void {
this.log(
`Flow coverage: ${result.coveragePercent}% (${result.coveredFlows} of ${result.totalFlows} active Flows and processes have a test). Threshold: ${result.threshold}%.`,
);
for (const line of coverageLines(result)) this.log(line);
if (result.ready) {
this.logSuccess('Ready: the org meets the coverage needed to deploy Flows as active.');
} else {
Expand Down Expand Up @@ -118,6 +116,7 @@ export default class FlowcovReadiness extends SfCommand<ReadinessResult> {
Tests: [...new Set(f.tests.map((t) => `${t.className}.${t.methodName}`))].join(', '),
})),
});
this.log(ELEMENT_COVERAGE_NOTE);
}

if (result.processBuilderProcesses > 0) {
Expand Down
9 changes: 3 additions & 6 deletions src/core/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describeElements } from './elementCoverage.js';
import { deployAsActiveAdvice } from './flowSettings.js';
import { isProcessBuilder, PROCESS_BUILDER_ADVICE, processTypeLabel } from './flowTypes.js';
import { coverageLines, ELEMENT_COVERAGE_NOTE } from './readiness.js';
import type { FlowTestRef, GateResult, ReadinessResult } from './types.js';

/** Output formats the `readiness` and `gate` commands print. */
Expand Down Expand Up @@ -47,12 +48,7 @@ export function readinessMarkdown(result: ReadinessResult): string {
const status = result.ready
? '**Ready**: the org meets the coverage needed to deploy Flows as active.'
: `**Not ready**: add Apex tests for ${result.flowsNeeded} more Flow(s) to reach ${result.threshold}%.`;
lines.push(
status,
'',
`Flow coverage: **${result.coveragePercent}%** (${result.coveredFlows} of ${result.totalFlows} active Flows and processes have a test). Threshold: ${result.threshold}%.`,
'',
);
lines.push(status, '', ...coverageLines(result, (text) => `**${text}**`).flatMap((line) => [line, '']));

const warnings = [...result.warnings];
if (result.deployAsActive !== undefined) {
Expand Down Expand Up @@ -114,6 +110,7 @@ export function readinessMarkdown(result: ReadinessResult): string {
]),
),
'',
ELEMENT_COVERAGE_NOTE,
);
}
return `${lines.join('\n').trimEnd()}\n`;
Expand Down
24 changes: 23 additions & 1 deletion src/core/orgCoverageSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type FlowFingerprint, fingerprintFromMetadata } from './flowFingerprint.js';
import { COUNTED_PROCESS_TYPES, qualifiedFlowName, splitFlowName } from './flowTypes.js';
import { COUNTED_PROCESS_TYPES, isCountedProcessType, qualifiedFlowName, splitFlowName } from './flowTypes.js';
import type { CoverageSource, FlowCoverage, FlowTestRef, ToolingQuery } from './types.js';

type FlowRecord = {
Expand Down Expand Up @@ -116,6 +116,28 @@ export class OrgCoverageSource implements CoverageSource {
}
}

public async countCoveredInactiveVersions(): Promise<number | undefined> {
try {
const rows = await this.query<{ FlowVersionId?: string }>('SELECT FlowVersionId FROM FlowTestCoverage');
const ids = [...new Set(rows.flatMap((r) => (r.FlowVersionId ? [r.FlowVersionId] : [])))];
const versions = await this.queryInChunks<{
Id: string;
Status?: string;
ProcessType?: string;
Definition?: { LatestVersionId?: string | null } | null;
}>(ids, (list) => `SELECT Id, Status, ProcessType, Definition.LatestVersionId FROM Flow WHERE Id IN (${list})`);
return versions.filter(
(v) =>
v.Status !== 'Active' &&
isCountedProcessType(v.ProcessType) &&
v.Definition?.LatestVersionId &&
idKey(v.Definition.LatestVersionId) === idKey(v.Id),
).length;
} catch {
return undefined;
}
}

public getFlowFingerprint(flowVersionId: string): Promise<FlowFingerprint | undefined> {
// The gate's content check and its uncovered-element list both ask for the same versions.
const key = idKey(flowVersionId);
Expand Down
44 changes: 41 additions & 3 deletions src/core/readiness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,42 @@ export type ReadinessOptions = {
dataQuery?: DataQuery;
};

/** Said under the covered Flows, since their element coverage reads like part of the requirement. */
export const ELEMENT_COVERAGE_NOTE =
"Element coverage shows how much of each Flow the tests run. It doesn't count toward the threshold: a Flow counts once any test runs it.";

/**
* The report's coverage lines: the headline the requirement goes by, then any details. `strong`
* marks the percentage (bold in Markdown).
*/
export function coverageLines(result: ReadinessResult, strong = (text: string): string => text): string[] {
const threshold = `Threshold: ${result.threshold}%.`;
const lines =
result.coveredInactiveVersions === 0
? [
`Flow coverage: ${strong(`${result.coveragePercent}%`)} (${result.coveredFlows} of ${result.totalFlows} active Flows and processes have a test). ${threshold}`,
]
: [
`Flow coverage: ${strong(`${result.salesforceCoveragePercent}%`)} as Salesforce calculates it (${result.coveredFlows + result.coveredInactiveVersions} of ${result.totalFlows + result.coveredInactiveVersions} Flows: ${result.coveredFlows} active and ${result.coveredInactiveVersions} inactive with a test, ${result.uncoveredFlows} active without one). ${threshold}`,
`Active Flows with a test: ${result.coveragePercent}% (${result.coveredFlows} of ${result.totalFlows}). Salesforce also counts the latest inactive versions that have a test.`,
];
if (result.thresholdAssumed) {
lines.push(
`${result.threshold}% is the usual requirement. Production's Process Automation Settings set the actual percentage: pass --threshold if it differs.`,
);
}
return lines;
}

/**
* Measures the org against the deploy-as-active coverage requirement: the share of
* active autolaunched Flows and processes that at least one Apex test runs.
* Measures the org against the deploy-as-active coverage requirement the way Salesforce
* calculates it: the Flow versions an Apex test covers (active ones, and latest inactive ones),
* divided by the active versions without coverage plus the versions with coverage. A Flow counts
* once any test runs it; how many of its elements run doesn't matter.
*/
export async function buildReadinessReport(options: ReadinessOptions): Promise<ReadinessResult> {
const threshold = options.threshold ?? DEFAULT_THRESHOLD;
const coveredInactiveVersions = (await options.source.countCoveredInactiveVersions?.()) ?? 0;
const coverage = await options.source.listActiveCountedFlows();

const flows: ReadinessFlow[] = [];
Expand Down Expand Up @@ -50,15 +80,23 @@ export async function buildReadinessReport(options: ReadinessOptions): Promise<R
const coveredFlows = flows.filter((f) => f.covered).length;
// With no active Flows to cover, Salesforce has nothing to block.
const coveragePercent = totalFlows === 0 ? 100 : Math.round((coveredFlows / totalFlows) * 1000) / 10;
const flowsNeeded = Math.max(0, Math.ceil((threshold * totalFlows) / 100) - coveredFlows);
// Covering an active Flow moves it from the uncovered part of the denominator to the covered
// part, so the denominator stays the same.
const covered = coveredFlows + coveredInactiveVersions;
const counted = totalFlows + coveredInactiveVersions;
const salesforceCoveragePercent = counted === 0 ? 100 : Math.round((covered / counted) * 1000) / 10;
const flowsNeeded = Math.max(0, Math.ceil((threshold * counted) / 100) - covered);
const warning = await missingCoverageWarning(options.source, totalFlows > 0 && coveredFlows === 0);

return {
threshold,
thresholdAssumed: options.threshold === undefined,
totalFlows,
coveredFlows,
uncoveredFlows: totalFlows - coveredFlows,
coveragePercent,
salesforceCoveragePercent,
coveredInactiveVersions,
ready: flowsNeeded === 0,
flowsNeeded,
processBuilderProcesses: flows.filter((f) => isProcessBuilder(f.processType)).length,
Expand Down
19 changes: 19 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export type CoverageSource = {
* Apex tests ran without code coverage on.
*/
hasAnyFlowCoverage?(): Promise<boolean>;
/**
* How many Flows count toward the requirement although no version of them is active: the latest
* version is inactive and a test covers it. Salesforce counts these as covered. Undefined when
* the source can't tell.
*/
countCoveredInactiveVersions?(): Promise<number | undefined>;
};

/** Runs a Tooling API SOQL query and returns every record (all pages). */
Expand Down Expand Up @@ -132,10 +138,23 @@ export type ReadinessFlow = {

export type ReadinessResult = {
threshold: number;
/** True when no threshold was given, so the usual 75% was used. Production can set another. */
thresholdAssumed: boolean;
/** Active counted Flows. */
totalFlows: number;
/** Active counted Flows a test runs. */
coveredFlows: number;
uncoveredFlows: number;
/** Active Flows with a test, 0-100. */
coveragePercent: number;
/**
* Coverage as Salesforce calculates it, 0-100: versions with coverage (active ones, and latest
* inactive ones) divided by active versions without coverage plus versions with coverage.
* `ready` and `flowsNeeded` go by this. Equals `coveragePercent` when no inactive version has coverage.
*/
salesforceCoveragePercent: number;
/** Latest inactive versions with coverage, which Salesforce counts as covered. */
coveredInactiveVersions: number;
ready: boolean;
/** Additional Flows that need a test to reach the threshold. */
flowsNeeded: number;
Expand Down
Loading
Loading