Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(ci): centralize jest selection policy (#119757)

Raúl Negrón-Otero5fe18ac885e7
diff --git a/.github/scripts/jest-select.js b/.github/scripts/jest-select.js
index 9baa294a98cc..0b4e399b05d6 100644
--- a/.github/scripts/jest-select.js
+++ b/.github/scripts/jest-select.js
@@ -1,28 +1,74 @@
#!/usr/bin/env node
-// Sizes the PR jest matrix from the count of tests related to the changed files: lists them the
-// way the jest job will, then writes the matrix and the counts to GITHUB_OUTPUT.
-//
-// Run with: CHANGED_FILES="src/a.ts ../products/b.ts" node .github/scripts/jest-select.js
-
const { execFileSync } = require('node:child_process')
const fs = require('node:fs')
const path = require('node:path')
const FRONTEND_DIR = path.resolve(__dirname, '../../frontend')
-// Initial limit from the probes documented in docs/internal/ci-things-already-tried.md.
// File counts approximate runtime; tune this from observed shard durations.
const TEST_FILES_PER_SHARD = 342
const MAX_SHARDS = 8
-const SEGMENT = 'app'
+const MAX_CHANGED_FILES = 200
-function listRelatedTests(changedFiles) {
- if (changedFiles.length === 0) {
- return []
+/** @typedef {{ mode: 'selective', changedFiles: string[], changedFileCount: number }} Candidate */
+/** @typedef {{ mode: 'full' | 'skip', changedFileCount: number | null, reason: { category: string, detail: string } }} Fallback */
+/** @typedef {(Candidate & { testCount: number }) | Fallback} Selection */
+
+/** @returns {Fallback} */
+function fallback(isDraft, changedFileCount, category, detail) {
+ // Drafts defer uncertain selection to their ready run; ready PRs need coverage now.
+ return { mode: isDraft ? 'skip' : 'full', changedFileCount, reason: { category, detail } }
+}
+
+/** @returns {Candidate | Fallback} */
+function classifyChanges({ files, diffOutcome, isDraft }) {
+ if (diffOutcome !== 'success' || files.length === 0) {
+ return fallback(isDraft, null, 'diff_unavailable', diffOutcome)
+ }
+ const changedFileCount = files.length
+ if (changedFileCount > MAX_CHANGED_FILES) {
+ return fallback(isDraft, changedFileCount, 'over_ceiling', `${changedFileCount} files`)
}
- // The package's `jest` script runs build:products first, like the jest job does.
- const args = ['jest', '--listTests', '--findRelatedTests', ...changedFiles]
- const output = execFileSync('pnpm', args, {
+ for (const file of files) {
+ if (
+ /^frontend\/jest\./.test(file) ||
+ /^tsconfig\..*\.json$/.test(file) ||
+ [
+ 'babel.config.js',
+ 'tsconfig.json',
+ 'package.json',
+ 'pnpm-lock.yaml',
+ 'webpack.config.js',
+ '.github/workflows/ci-frontend.yml',
+ '.github/scripts/jest-retries.cjs',
+ '.github/scripts/jest-select.js',
+ 'patches/[email protected]',
+ '.nvmrc',
+ ].includes(file)
+ ) {
+ return fallback(isDraft, changedFileCount, 'config_change', file)
+ }
+ // These dependencies are loaded outside Jest's runtime import graph.
+ if (
+ file.startsWith('common/replay-shared/src/') ||
+ file.startsWith('frontend/src/mocks/') ||
+ file === 'frontend/src/lib/api.mock.ts'
+ ) {
+ return fallback(isDraft, changedFileCount, 'unindexed_dependency', file)
+ }
+ }
+ const sources = files
+ .filter((file) => /\.(ts|tsx|js|jsx|mjs|cjs|json)$/.test(file))
+ .filter((file) => /^(frontend|products|common|ee\/frontend)\//.test(file))
+ .map((file) => (file.startsWith('frontend/') ? file.slice('frontend/'.length) : `../${file}`))
+ // Listing must use the same space-separated arguments as the test job.
+ const changedFiles = sources.join(' ').split(' ').filter(Boolean)
+ return { mode: 'selective', changedFiles, changedFileCount }
+}
+
+function listRelatedTests(changedFiles) {
+ // The package's jest command runs build:products, as the test job does.
+ const output = execFileSync('pnpm', ['jest', '--listTests', '--findRelatedTests', ...changedFiles], {
cwd: FRONTEND_DIR,
encoding: 'utf8',
timeout: 2 * 60 * 1000,
@@ -32,6 +78,26 @@ function listRelatedTests(changedFiles) {
return output.split('\n').filter((line) => line.startsWith('/'))
}
+/** @returns {Selection} */
+function selectTests(input, findRelatedTests) {
+ const candidate = classifyChanges(input)
+ if (candidate.mode !== 'selective') {
+ return candidate
+ }
+ if (candidate.changedFiles.length === 0) {
+ return { ...candidate, testCount: 0 }
+ }
+ if (input.installOutcome === 'success') {
+ try {
+ return { ...candidate, testCount: findRelatedTests(candidate.changedFiles).length }
+ } catch (error) {
+ console.error(`Test selection failed: ${error.message}`)
+ console.log('::notice::Test selection failed; using the prepared fallback')
+ }
+ }
+ return fallback(input.isDraft, candidate.changedFileCount, 'selector_crash', 'Jest selection unavailable')
+}
+
function planShards(testCount) {
return Math.min(MAX_SHARDS, Math.ceil(testCount / TEST_FILES_PER_SHARD))
}
@@ -42,7 +108,7 @@ function planOutputs({ changedFiles, changedFileCount, testCount }) {
mode: 'selective',
matrix: JSON.stringify({
include: Array.from({ length: shardCount }, (_, i) => ({
- segment: SEGMENT,
+ segment: 'app',
chunk: i + 1,
shard_count: shardCount,
})),
@@ -58,19 +124,79 @@ function planOutputs({ changedFiles, changedFileCount, testCount }) {
}
}
+/** @param {Selection} selection */
+function workflowOutputs(selection) {
+ if (selection.mode === 'selective') {
+ return planOutputs(selection)
+ }
+ return {
+ mode: selection.mode,
+ // The test job supplies its full matrix when this output is empty.
+ matrix: selection.mode === 'full' ? '' : '{"include":[]}',
+ should_run: String(selection.mode === 'full'),
+ changed_files: '',
+ changed_file_count: String(selection.changedFileCount ?? ''),
+ selected_file_count: '0',
+ shard_count: selection.mode === 'full' ? '4' : '0',
+ full_run_reason_category: selection.reason.category,
+ full_run_reason_detail: selection.reason.detail,
+ }
+}
+
function main() {
- const changedFiles = (process.env.CHANGED_FILES ?? '').split(' ').filter(Boolean)
- const testCount = listRelatedTests(changedFiles).length
- const outputs = planOutputs({ changedFiles, changedFileCount: process.env.CHANGED_FILE_COUNT ?? '', testCount })
- const lines = Object.entries(outputs).map(([key, value]) => `${key}=${value}\n`)
- fs.appendFileSync(process.env.GITHUB_OUTPUT ?? '/dev/stdout', lines.join(''))
- console.log(
- `Selective mode: ${changedFiles.length} changed files, ${testCount} related tests, ${outputs.shard_count} shards`
+ let outputs
+ if (process.argv[2] === undefined) {
+ const changedFiles = (process.env.CHANGED_FILES ?? '').split(' ').filter(Boolean)
+ const testCount = changedFiles.length === 0 ? 0 : listRelatedTests(changedFiles).length
+ outputs = planOutputs({ changedFiles, changedFileCount: process.env.CHANGED_FILE_COUNT ?? '', testCount })
+ } else {
+ const filesPath = process.env.CHANGED_FILES_PATH ?? '/tmp/changed.txt'
+ const input = {
+ files: fs.existsSync(filesPath) ? fs.readFileSync(filesPath, 'utf8').split('\n').filter(Boolean) : [],
+ diffOutcome: process.env.DIFF_OUTCOME ?? '',
+ isDraft: process.env.PR_DRAFT === 'true',
+ installOutcome: process.env.INSTALL_OUTCOME ?? '',
+ }
+ if (process.argv[2] === 'prepare') {
+ const candidate = classifyChanges(input)
+ outputs = { needs_dependencies: candidate.mode === 'selective' && candidate.changedFiles.length > 0 }
+ } else if (process.argv[2] === 'select') {
+ const selection = selectTests(input, listRelatedTests)
+ if (selection.mode !== 'selective' && selection.reason.category !== 'selector_crash') {
+ const behavior =
+ selection.mode === 'skip'
+ ? 'draft skips jest (its ready run selects again)'
+ : 'running the full jest matrix'
+ console.log(`::notice::${selection.reason.category} (${selection.reason.detail}); ${behavior}`)
+ }
+ outputs = workflowOutputs(selection)
+ } else {
+ throw new Error('Expected prepare or select, or no command for CHANGED_FILES input')
+ }
+ }
+ fs.appendFileSync(
+ process.env.GITHUB_OUTPUT ?? '/dev/stdout',
+ Object.entries(outputs)
+ .map(([key, value]) => `${key}=${value}\n`)
+ .join('')
)
+ if (outputs.mode === 'selective') {
+ console.log(
+ `Selective mode: ${outputs.selected_file_count} changed files, ${outputs.related_test_count} related tests, ${outputs.shard_count} shards`
+ )
+ }
}
if (require.main === module) {
main()
}
-module.exports = { planShards, planOutputs, TEST_FILES_PER_SHARD, MAX_SHARDS }
+module.exports = {
+ classifyChanges,
+ selectTests,
+ workflowOutputs,
+ planShards,
+ planOutputs,
+ TEST_FILES_PER_SHARD,
+ MAX_SHARDS,
+}
diff --git a/.github/scripts/jest-select.test.js b/.github/scripts/jest-select.test.js
index 3cb9abb8c01f..c27b096f61ce 100644
--- a/.github/scripts/jest-select.test.js
+++ b/.github/scripts/jest-select.test.js
@@ -1,40 +1,219 @@
-// Run with: node --test .github/scripts/jest-select.test.js
-
const test = require('node:test')
const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const fs = require('node:fs')
+const os = require('node:os')
+const path = require('node:path')
+
+const { classifyChanges, selectTests, workflowOutputs } = require('./jest-select.js')
+
+const ready = { diffOutcome: 'success', isDraft: false }
-const { planShards, planOutputs, TEST_FILES_PER_SHARD, MAX_SHARDS } = require('./jest-select.js')
+for (const [name, input, category] of [
+ ['failed diff', { files: ['frontend/src/a.ts'], diffOutcome: 'failure' }, 'diff_unavailable'],
+ ['empty diff', { files: [] }, 'diff_unavailable'],
+ ['oversized diff', { files: Array.from({ length: 201 }, (_, i) => `frontend/src/${i}.ts`) }, 'over_ceiling'],
+ ['Jest config', { files: ['frontend/jest.config.js'] }, 'config_change'],
+ ['TypeScript config', { files: ['tsconfig.paths.json'] }, 'config_change'],
+ ['lockfile', { files: ['pnpm-lock.yaml'] }, 'config_change'],
+ ['selector itself', { files: ['.github/scripts/jest-select.js'] }, 'config_change'],
+ ['Jest setup dependency', { files: ['frontend/src/mocks/handlers.ts'] }, 'unindexed_dependency'],
+ ['API mock', { files: ['frontend/src/lib/api.mock.ts'] }, 'unindexed_dependency'],
+ ['replay shared', { files: ['common/replay-shared/src/index.ts'] }, 'unindexed_dependency'],
+]) {
+ for (const isDraft of [false, true]) {
+ test(`${name} ${isDraft ? 'defers a draft' : 'runs the full ready PR matrix'}`, () => {
+ const selection = classifyChanges({ ...ready, ...input, isDraft })
+ assert.equal(selection.mode, isDraft ? 'skip' : 'full')
+ assert.equal(selection.reason.category, category)
+ const outputs = workflowOutputs(selection)
+ assert.equal(outputs.should_run, String(!isDraft))
+ assert.equal(outputs.matrix, isDraft ? '{"include":[]}' : '')
+ })
+ }
+}
-test('planShards adds a shard when the file limit is exceeded', () => {
- assert.equal(planShards(TEST_FILES_PER_SHARD), 1)
- assert.equal(planShards(TEST_FILES_PER_SHARD + 1), 2)
- assert.equal(planShards(2000), 6)
+test('selection resolves source paths from the frontend package and retains the total diff count', () => {
+ assert.deepEqual(
+ classifyChanges({
+ ...ready,
+ files: [
+ 'frontend/src/a.ts',
+ 'products/example/frontend/a.tsx',
+ 'common/example/data.json',
+ 'ee/frontend/a.js',
+ 'frontend/src/a.scss',
+ 'posthog/example.py',
+ 'docs/example.md',
+ ],
+ }),
+ {
+ mode: 'selective',
+ changedFiles: [
+ 'src/a.ts',
+ '../products/example/frontend/a.tsx',
+ '../common/example/data.json',
+ '../ee/frontend/a.js',
+ ],
+ changedFileCount: 7,
+ }
+ )
})
-test('planShards caps the count at MAX_SHARDS', () => {
- assert.equal(planShards(100000), MAX_SHARDS)
+test('the changed-file ceiling includes its boundary', () => {
+ const files = Array.from({ length: 200 }, (_, i) => `frontend/src/${i}.ts`)
+ assert.equal(classifyChanges({ ...ready, files }).mode, 'selective')
})
-test('planOutputs skips the jest job when no test is related', () => {
- const outputs = planOutputs({ changedFiles: ['src/types-only.ts'], changedFileCount: 1, testCount: 0 })
+for (const [testCount, shardCount] of [
+ [0, 0],
+ [342, 1],
+ [343, 2],
+ [1200, 4],
+ [2000, 6],
+ [100000, 8],
+]) {
+ test(`${testCount} related tests use ${shardCount} shards`, () => {
+ const selection = selectTests({ ...ready, files: ['frontend/src/a.ts'], installOutcome: 'success' }, () =>
+ Array(testCount)
+ )
+ const outputs = workflowOutputs(selection)
+ assert.equal(outputs.shard_count, String(shardCount))
+ assert.equal(outputs.should_run, String(shardCount > 0))
+ assert.equal(outputs.changed_file_count, '1')
+ assert.equal(outputs.related_test_count, String(testCount))
+ assert.deepEqual(JSON.parse(outputs.matrix), {
+ include: Array.from({ length: shardCount }, (_, i) => ({
+ segment: 'app',
+ chunk: i + 1,
+ shard_count: shardCount,
+ })),
+ })
+ })
+}
- assert.equal(outputs.should_run, 'false')
- assert.equal(outputs.shard_count, '0')
- assert.deepEqual(JSON.parse(outputs.matrix), { include: [] })
+for (const isDraft of [false, true]) {
+ for (const failure of ['install', 'listing']) {
+ test(`${failure} failure ${isDraft ? 'defers a draft' : 'runs all tests on a ready PR'}`, () => {
+ const selection = selectTests(
+ {
+ ...ready,
+ isDraft,
+ files: ['frontend/src/a.ts'],
+ installOutcome: failure === 'install' ? 'failure' : 'success',
+ },
+ () => {
+ throw new Error('test listing unavailable')
+ }
+ )
+ assert.deepEqual(selection, {
+ mode: isDraft ? 'skip' : 'full',
+ changedFileCount: 1,
+ reason: { category: 'selector_crash', detail: 'Jest selection unavailable' },
+ })
+ })
+ }
+}
+
+test('a diff without Jest sources needs neither dependencies nor test listing', () => {
+ const selection = selectTests({ ...ready, files: ['frontend/src/a.scss'], installOutcome: 'skipped' }, () => {
+ assert.fail('must not invoke Jest for an empty source selection')
+ })
+ assert.equal(selection.mode, 'selective')
+ assert.equal(workflowOutputs(selection).should_run, 'false')
})
-test('planOutputs emits one matrix cell per shard with the shared shard_count', () => {
- const outputs = planOutputs({ changedFiles: ['src/a.ts', 'src/b.ts'], changedFileCount: 7, testCount: 1200 })
- const matrix = JSON.parse(outputs.matrix)
+test('the CLI prepares dependency installation and writes the final fallback without temporary state', () => {
+ const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'jest-selection-'))
+ try {
+ const changedFiles = path.join(directory, 'changed.txt')
+ fs.writeFileSync(changedFiles, 'frontend/src/a.ts\n')
+ for (const [args, expected] of [
+ [['prepare'], { needs_dependencies: 'true' }],
+ [
+ ['select'],
+ {
+ mode: 'full',
+ should_run: 'true',
+ changed_file_count: '1',
+ full_run_reason_category: 'selector_crash',
+ },
+ ],
+ ]) {
+ const output = path.join(directory, args[0])
+ const result = spawnSync(process.execPath, [path.join(__dirname, 'jest-select.js'), ...args], {
+ encoding: 'utf8',
+ env: {
+ CHANGED_FILES_PATH: changedFiles,
+ DIFF_OUTCOME: 'success',
+ PR_DRAFT: 'false',
+ INSTALL_OUTCOME: 'failure',
+ GITHUB_OUTPUT: output,
+ },
+ })
+ assert.deepEqual({ status: result.status, stderr: result.stderr }, { status: 0, stderr: '' })
+ const outputs = Object.fromEntries(
+ fs
+ .readFileSync(output, 'utf8')
+ .trimEnd()
+ .split('\n')
+ .map((line) => {
+ const equals = line.indexOf('=')
+ return [line.slice(0, equals), line.slice(equals + 1)]
+ })
+ )
+ for (const [key, value] of Object.entries(expected)) {
+ assert.equal(outputs[key], value)
+ }
+ }
+ } finally {
+ fs.rmSync(directory, { recursive: true, force: true })
+ }
+})
- assert.equal(outputs.should_run, 'true')
- assert.equal(outputs.selected_file_count, '2')
- assert.equal(outputs.changed_file_count, '7')
- assert.equal(outputs.related_test_count, '1200')
- assert.equal(String(matrix.include.length), outputs.shard_count)
- assert.deepEqual(matrix.include[0], { segment: 'app', chunk: 1, shard_count: matrix.include.length })
- assert.deepEqual(
- matrix.include.map((cell) => cell.chunk),
- matrix.include.map((_, i) => i + 1)
- )
+test("listing keeps the test job's existing space-separated argument contract", () => {
+ const selection = selectTests({ ...ready, files: ['frontend/src/a b.ts'], installOutcome: 'success' }, (files) => {
+ assert.deepEqual(files, ['src/a', 'b.ts'])
+ return []
+ })
+ assert.equal(workflowOutputs(selection).selected_file_count, '2')
})
+
+for (const [name, changedFiles, listingExit, expectedCount] of [
+ ['success', 'src/a.ts ../products/example/a.tsx', 0, '2'],
+ ['empty input', '', 1, '0'],
+ ['listing failure', 'src/a.ts', 1, null],
+]) {
+ test(`the original CHANGED_FILES CLI retains its ${name} behavior`, () => {
+ const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'jest-selection-cli-'))
+ try {
+ fs.writeFileSync(
+ path.join(directory, 'pnpm'),
+ '#!/bin/sh\nprintf "/test-a\\n/test-b\\n"\nexit "$LISTING_EXIT"\n',
+ { mode: 0o755 }
+ )
+ const output = path.join(directory, 'output')
+ const result = spawnSync(process.execPath, [path.join(__dirname, 'jest-select.js')], {
+ encoding: 'utf8',
+ env: {
+ PATH: directory,
+ CHANGED_FILES: changedFiles,
+ CHANGED_FILE_COUNT: '7',
+ LISTING_EXIT: String(listingExit),
+ GITHUB_OUTPUT: output,
+ },
+ })
+ if (expectedCount === null) {
+ assert.equal(result.status, 1)
+ assert.equal(fs.existsSync(output), false)
+ } else {
+ assert.deepEqual({ status: result.status, stderr: result.stderr }, { status: 0, stderr: '' })
+ assert.match(fs.readFileSync(output, 'utf8'), new RegExp(`related_test_count=${expectedCount}\\n`))
+ assert.match(fs.readFileSync(output, 'utf8'), /changed_file_count=7\n/)
+ assert.match(result.stdout, /Selective mode:/)
+ }
+ } finally {
+ fs.rmSync(directory, { recursive: true, force: true })
+ }
+ })
+}
diff --git a/.github/workflows/ci-frontend.yml b/.github/workflows/ci-frontend.yml
index d3a7fb75f25a..b01d59dbffb6 100644
--- a/.github/workflows/ci-frontend.yml
+++ b/.github/workflows/ci-frontend.yml
@@ -324,107 +324,11 @@ jobs:
env:
DIFF_OUTCOME: ${{ steps.diff.outcome }}
PR_DRAFT: ${{ github.event.pull_request.draft }}
- run: |
- set -euo pipefail
-
- # Use this whenever the selector can't reason about the diff — a
- # narrowed run it can't vouch for would be false confidence.
- # A draft skips jest and defers to its ready run; a ready PR has no
- # later run on this PR to defer to, so it takes the full matrix.
- # Mirrors the draft/ready fallback turbo-discover.js applies to the
- # Django matrices. The run-ci-frontend label forces the full matrix.
- # $1 is a closed category for grouping the telemetry, $2 the unbounded
- # detail behind it.
- fall_back() {
- local mode should_run matrix shards
- if [[ "$PR_DRAFT" == "true" ]]; then
- echo "::notice::$1 ($2); draft skips jest (its ready run selects again)"
- mode=skip should_run=false shards=0
- matrix='{"include":[]}'
- else
- # Empty matrix: the jest job's own default supplies the full
- # fanout, so the shard shape is written down in one place.
- echo "::notice::$1 ($2); running the full jest matrix"
- mode=full should_run=true shards=4
- matrix=
- fi
- {
- echo "mode=$mode"
- echo "matrix=$matrix"
- echo "should_run=$should_run"
- echo "changed_files="
- echo "changed_file_count=${changed_count:-}"
- echo "selected_file_count=0"
- echo "shard_count=$shards"
- echo "full_run_reason_category=$1"
- echo "full_run_reason_detail=$2"
- } > /tmp/jest-selection-fallback
- }
-
- if [[ "$DIFF_OUTCOME" != "success" ]] || [[ ! -s /tmp/changed.txt ]]; then
- fall_back diff_unavailable "${DIFF_OUTCOME}"
- exit 0
- fi
-
- # tr: the count is reported as telemetry now, and BSD wc pads it.
- changed_count=$(wc -l < /tmp/changed.txt | tr -d ' ')
- if [[ "$changed_count" -gt 200 ]]; then
- fall_back over_ceiling "$changed_count files"
- exit 0
- fi
-
- # Skip selection when jest config / build / lock files change, or
- # when a file changes whose consumers --findRelatedTests misses.
- # replay-shared sits outside the frontend jest roots. Jest loads
- # the MSW mocks for every test through a setup file, so no test
- # has to import them. Pattern matches mirror the frontend_code
- # paths-filter in `changes`.
- while IFS= read -r f; do
- case "$f" in
- frontend/jest.*|babel.config.js|tsconfig.json|tsconfig.*.json|\
- package.json|pnpm-lock.yaml|webpack.config.js|\
- .github/workflows/ci-frontend.yml|.github/scripts/jest-retries.cjs|.github/scripts/jest-select.js|patches/[email protected]|.nvmrc)
- fall_back config_change "$f"
- exit 0
- ;;
- common/replay-shared/src/*|frontend/src/mocks/*|frontend/src/lib/api.mock.ts)
- fall_back unindexed_dependency "$f"
- exit 0
- ;;
- esac
- done < /tmp/changed.txt
-
- # Filter the diff to source files jest can resolve. Any code under
- # frontend/, products/, common/, ee/frontend/ — anything else (docs,
- # workflows, backend) doesn't reach the jest module graph. Convert
- # repo-root paths to paths relative to the @posthog/frontend package
- # cwd because `pnpm --filter=@posthog/frontend exec` runs from there.
- CHANGED_FILES=$(
- awk '
- /\.(ts|tsx|js|jsx|mjs|cjs|json)$/ {
- if ($0 ~ /^frontend\//) {
- sub(/^frontend\//, "", $0)
- print
- next
- }
- if ($0 ~ /^(products|common|ee\/frontend)\//) {
- print "../" $0
- }
- }
- ' /tmp/changed.txt | tr '\n' ' ' | sed 's/ $//'
- )
-
- # Preserve the same fallback if dependency installation or test listing fails.
- fall_back selector_crash "Jest selection unavailable" > /dev/null
- {
- echo "selective=true"
- echo "changed_files=$CHANGED_FILES"
- echo "changed_file_count=$changed_count"
- } >> "$GITHUB_OUTPUT"
+ run: node .github/scripts/jest-select.js prepare
- name: Mint setup-action GitHub token
id: setup-gh-token
- if: steps.prepare.outputs.selective == 'true' && steps.prepare.outputs.changed_files != '' && github.event.pull_request.head.repo.full_name == github.repository
+ if: steps.prepare.outputs.needs_dependencies == 'true' && github.event.pull_request.head.repo.full_name == github.repository
continue-on-error: true
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
with:
@@ -434,9 +338,9 @@ jobs:
- name: Install pnpm dependencies
id: selector-install
- if: steps.prepare.outputs.selective == 'true' && steps.prepare.outputs.changed_files != ''
+ if: steps.prepare.outputs.needs_dependencies == 'true'
timeout-minutes: 5
- # Allow the final step to publish the prepared fallback if setup fails.
+ # A failed install falls back to full coverage, or defers a draft to its ready run.
continue-on-error: true
uses: ./.github/actions/pnpm-install
with:
@@ -446,16 +350,10 @@ jobs:
- name: Build narrowed matrix
id: classify
env:
- SELECTIVE: ${{ steps.prepare.outputs.selective }}
+ DIFF_OUTCOME: ${{ steps.diff.outcome }}
+ PR_DRAFT: ${{ github.event.pull_request.draft }}
INSTALL_OUTCOME: ${{ steps.selector-install.outcome }}
- CHANGED_FILES: ${{ steps.prepare.outputs.changed_files }}
- CHANGED_FILE_COUNT: ${{ steps.prepare.outputs.changed_file_count }}
- run: |
- if [[ "$SELECTIVE" == "true" ]] && [[ -z "$CHANGED_FILES" || "$INSTALL_OUTCOME" == "success" ]]; then
- node .github/scripts/jest-select.js && exit 0
- echo "::notice::Test selection failed; using the prepared fallback"
- fi
- cat /tmp/jest-selection-fallback >> "$GITHUB_OUTPUT"
+ run: node .github/scripts/jest-select.js select
frontend-format:
name: Frontend formatting
diff --git a/tools/workflow-plan/tests/workflows.test.ts b/tools/workflow-plan/tests/workflows.test.ts
index afd33d824ff2..1976bad9508d 100644
--- a/tools/workflow-plan/tests/workflows.test.ts
+++ b/tools/workflow-plan/tests/workflows.test.ts
@@ -1,8 +1,6 @@
// These tests check the CI workflows, not the planner. A failure here means a
// job condition in a workflow file changed what runs; the planner itself is covered by plan.test.ts.
-import { spawnSync } from 'node:child_process'
-import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
-import os from 'node:os'
+import { readdirSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
@@ -477,11 +475,11 @@ const STEP_EXPECTATIONS: StepExpectation[] = [
},
]),
...[
- { name: 'known fallback', selective: '', changedFiles: '', fork: false },
- { name: 'empty source selection', selective: 'true', changedFiles: '', fork: false },
- { name: 'selective PR', selective: 'true', changedFiles: 'src/example.ts', fork: false },
- { name: 'selective fork PR', selective: 'true', changedFiles: 'src/example.ts', fork: true },
- ].flatMap(({ name, selective, changedFiles, fork }) =>
+ { name: 'known fallback', needsDependencies: 'false', fork: false },
+ { name: 'empty source selection', needsDependencies: 'false', fork: false },
+ { name: 'selective PR', needsDependencies: 'true', fork: false },
+ { name: 'selective fork PR', needsDependencies: 'true', fork: true },
+ ].flatMap(({ name, needsDependencies, fork }) =>
['setup-gh-token', 'selector-install'].map((step) => ({
file: 'ci-frontend.yml',
job: 'select-jest-tests',
@@ -491,10 +489,10 @@ const STEP_EXPECTATIONS: StepExpectation[] = [
github: pullRequest({ fork }),
steps: {
...allFiltersChanged(workflow('ci-frontend.yml')),
- 'select-jest-tests': { prepare: { outputs: { selective, changed_files: changedFiles } } },
+ 'select-jest-tests': { prepare: { outputs: { needs_dependencies: needsDependencies } } },
},
},
- runs: selective === 'true' && changedFiles !== '' && (step === 'selector-install' || !fork),
+ runs: needsDependencies === 'true' && (step === 'selector-install' || !fork),
}))
),
{
@@ -569,47 +567,6 @@ describe('CI workflow run plans', () => {
}
})
- it.each([
- ['failed install', 'failure', 'src/example.ts', 'full'],
- ['empty source selection', 'skipped', '', 'selective'],
- ['successful install', 'success', 'src/example.ts', 'selective'],
- ])('Jest selection after %s', (_name, installOutcome, changedFiles, mode) => {
- const script = workflow('ci-frontend.yml').jobs['select-jest-tests']?.steps?.find(
- (step) => step.id === 'classify'
- )?.run
- if (!script) {
- throw new Error('Missing Jest matrix classification script')
- }
- const directory = mkdtempSync(path.join(os.tmpdir(), 'jest-selection-'))
- const output = path.join(directory, 'output')
- const fallback = path.join(directory, 'fallback')
- try {
- writeFileSync(fallback, 'mode=full\nshould_run=true\n')
- const result = spawnSync(
- 'bash',
- [
- '-e',
- '-c',
- `node() { printf 'mode=selective\\nshould_run=false\\n' >> "$GITHUB_OUTPUT"; }\n${script.replaceAll('/tmp/jest-selection-fallback', fallback)}`,
- ],
- {
- encoding: 'utf8',
- env: {
- PATH: process.env.PATH,
- SELECTIVE: 'true',
- INSTALL_OUTCOME: installOutcome,
- CHANGED_FILES: changedFiles,
- GITHUB_OUTPUT: output,
- },
- }
- )
- expect({ status: result.status, stderr: result.stderr }).toEqual({ status: 0, stderr: '' })
- expect(readFileSync(output, 'utf8')).toBe(`mode=${mode}\nshould_run=${mode === 'full'}\n`)
- } finally {
- rmSync(directory, { recursive: true, force: true })
- }
- })
-
it.each([
['new bump', workflowDispatch(), 'bump', 'success', 'pass', true, false],
['missing image', workflowDispatch(), 'bump', 'failure', 'pass', false, false],