The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Report frontend jest test failures to ci conductor (#76666)
Nemanja Glumacf3370e8c8760
diff --git a/.github/actions/report-failures-to-ci-conductor/action.yml b/.github/actions/report-failures-to-ci-conductor/action.ymlindex 43d87a7b50a..6124fa3b5c0 100644--- a/.github/actions/report-failures-to-ci-conductor/action.yml+++ b/.github/actions/report-failures-to-ci-conductor/action.yml@@ -8,8 +8,8 @@ inputs:test-suite:description: Per-job identity label (CI_CONDUCTOR_TEST_SUITE); unique per job/leg.required: true- entrypoint:- description: bun reporter to run, e.g. release/ci-conductor/src/report-backend.ts.+ adapter:+ description: Which reporter to run; resolves to report-<adapter>.ts. backend or frontend.required: trueruns:@@ -37,4 +37,11 @@ runs:COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.ref_name }}CI_CONDUCTOR_TEST_SUITE: ${{ inputs.test-suite }}- run: bun ${{ inputs.entrypoint }}+ ADAPTER: ${{ inputs.adapter }}+ # Keep the path constrained to known entrypoints.+ run: |+ case "$ADAPTER" in+ backend|frontend) ;;+ *) echo "::error::Unknown ci-conductor adapter '$ADAPTER'"; exit 1 ;;+ esac+ bun "release/ci-conductor/src/report-${ADAPTER}.ts"diff --git a/.github/actions/test-driver/action.yml b/.github/actions/test-driver/action.ymlindex 6e1f843a4e4..d36bff76e41 100644--- a/.github/actions/test-driver/action.yml+++ b/.github/actions/test-driver/action.yml@@ -60,7 +60,7 @@ runs:uses: ./.github/actions/report-failures-to-ci-conductorwith:test-suite: ${{ inputs.junit-name }}- entrypoint: release/ci-conductor/src/report-backend.ts+ adapter: backend- name: Publish Test Report (JUnit)uses: dorny/test-reporter@a43b3a5f7366b97d083190328d2c652e1a8b6aa2 # v3.0.0# As per the documentation: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts#steps-contextdiff --git a/.github/workflows/backend.yml b/.github/workflows/backend.ymlindex a4e0a12daac..ff156abc451 100644--- a/.github/workflows/backend.yml+++ b/.github/workflows/backend.yml@@ -212,7 +212,7 @@ jobs:uses: ./.github/actions/report-failures-to-ci-conductorwith:test-suite: be-tests-java-${{ matrix.java-version }}-${{ matrix.job.test-group-name }}- entrypoint: release/ci-conductor/src/report-backend.ts+ adapter: backend- name: Upload Test Resultsuses: ./.github/actions/upload-test-resultsdiff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.ymlindex f3edbe7f8ad..15bfcbc00b0 100644--- a/.github/workflows/frontend.yml+++ b/.github/workflows/frontend.yml@@ -31,6 +31,13 @@ concurrency:group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}-frontendcancel-in-progress: true+env:+ # Consumed by the report-failures-to-ci-conductor step in fe-tests-unit. Absent+ # (e.g. fork PRs without secret access) the reporter no-ops. Matches the other+ # suite workflows (backend, drivers, e2e, app-db, semantic-search).+ CI_CONDUCTOR_BASE_URL: ${{ secrets.CI_CONDUCTOR_BASE_URL }}+ CI_CONDUCTOR_WEBHOOK_SECRET: ${{ secrets.CI_CONDUCTOR_WEBHOOK_SECRET }}+jobs:fe-lint:if: ${{ !inputs.skip-lint }}@@ -96,6 +103,10 @@ jobs:uses: ./.github/actions/prepare-frontend- name: Prepare back-end environmentuses: ./.github/actions/prepare-backend+ - name: Resolve current job id+ # Resolve JOB_ID before the long test step so the ci-conductor reporter+ # below can link failures to this exact job. Best-effort.+ uses: ./.github/actions/resolve-job-id- name: Run frontend unit testsid: fe-testsrun: bun run test-unit --silent --shard=${{ matrix.shard }}/${{ env.SHARDS }}@@ -103,6 +114,17 @@ jobs:# Even if this step fails we let it pass, and then `trunk-io/analytics-uploader` determines# whether to fail or to pass the whole job, based on the quarantine list.continue-on-error: true+ - name: Report failed tests to ci-conductor+ # Positioned between the test run and "Upload Test Results" so it reads+ # pristine JUnit (before Trunk quarantine processing). Gated to non-clean+ # runs + continue-on-error so it can never change the job outcome — Trunk+ # Quarantine remains the sole arbiter. See DEV-2224 / DEV-2235.+ if: ${{ always() && steps.fe-tests.outcome != 'success' }}+ continue-on-error: true+ uses: ./.github/actions/report-failures-to-ci-conductor+ with:+ test-suite: fe-tests-unit+ adapter: frontend- name: Upload Test Resultsuses: ./.github/actions/upload-test-resultsif: always() && github.event_name != 'workflow_dispatch'diff --git a/e2e/support/ci_conductor.unit.spec.ts b/e2e/support/ci_conductor.unit.spec.tsindex 82c73fd7763..f0add6a63f7 100644--- a/e2e/support/ci_conductor.unit.spec.ts+++ b/e2e/support/ci_conductor.unit.spec.ts@@ -386,6 +386,10 @@ describe("reportFailedTestsToConductor", () => {REPO_ID: "123",GITHUB_RUN_ID: "456",GITHUB_RUN_ATTEMPT: "2",+ // Cleared explicitly: the resolve-job-id action exports a real JOB_ID to+ // $GITHUB_ENV, so on CI this var is present in the ambient process env.+ // This case asserts the null-job_id path, so it must not inherit it.+ JOB_ID: undefined,CYPRESS_RETRIES: "1",COMMIT_SHA: "abc123",TARGET_BRANCH: "master",diff --git a/jest.config.js b/jest.config.jsindex a46d7f93a6c..b9c0d4e14cf 100644--- a/jest.config.js+++ b/jest.config.js@@ -137,7 +137,12 @@ const baseConfig = {/** @type {import('jest').Config} */const config = {- reporters: ["default", "jest-junit"],+ // `addFileAttribute` makes jest-junit emit the source path as a `file`+ // attribute on each <testcase>. Additive — it leaves classname/name untouched,+ // so Trunk's existing test identity is preserved — and it lets both Trunk+ // (codeowners/file attribution) and the ci-conductor reporter resolve a real+ // source file. Output dir/name come from the JEST_JUNIT_OUTPUT_* env vars.+ reporters: ["default", ["jest-junit", { addFileAttribute: "true" }]],coverageReporters: ["html", "lcov"],watchPlugins: ["jest-watch-typeahead/filename",diff --git a/release/ci-conductor/src/adapters/backend.ts b/release/ci-conductor/src/adapters/backend.tsindex 6efef3ee2b9..6925776a890 100644--- a/release/ci-conductor/src/adapters/backend.ts+++ b/release/ci-conductor/src/adapters/backend.ts@@ -6,26 +6,17 @@// (`../junit.ts`); this owns the backend-specific parts — hawk's `*_test.xml`// file layout — while the suite label is supplied by the entrypoint.-import { readFileSync, readdirSync } from "node:fs";-import { join } from "node:path";+import { readFileSync } from "node:fs";import type { NormalizedTest } from "../contract.ts";-import { parseJunit } from "../junit.ts";+import { findJunitFiles, parseJunit } from "../junit.ts";import { log } from "../util.ts";const JUNIT_DIR = process.env.JUNIT_DIR || "target/junit";-/** Recursively list `*_test.xml` files under `dir`. Returns [] on any error. */-function findJunitFiles(dir: string): string[] {- try {- return readdirSync(dir, { recursive: true })- .map((entry) => String(entry))- .filter((entry) => entry.endsWith("_test.xml"))- .map((entry) => join(dir, entry));- } catch {- return [];- }-}+/** hawk always names its JUnit files `*_test.xml`. */+const selectHawkJunit = (entries: string[]): string[] =>+ entries.filter((entry) => entry.endsWith("_test.xml"));/*** Normalize every JUnit file hawk wrote under `dir` into `NormalizedTest[]` —@@ -35,7 +26,7 @@ function findJunitFiles(dir: string): string[] {export function normalizeBackendJunit(dir: string = JUNIT_DIR,): NormalizedTest[] {- const files = findJunitFiles(dir);+ const files = findJunitFiles(dir, selectHawkJunit);const failures = files.flatMap((file) => {try {return parseJunit(readFileSync(file, "utf8"));diff --git a/release/ci-conductor/src/adapters/frontend.ts b/release/ci-conductor/src/adapters/frontend.tsnew file mode 100644index 00000000000..50a2c9d3d1c--- /dev/null+++ b/release/ci-conductor/src/adapters/frontend.ts@@ -0,0 +1,68 @@+// Frontend (jest) adapter: jest-junit's JUnit XML → NormalizedTest[].+//+// jest-junit writes a single JUnit file per job, its location given by jest's+// own `JEST_JUNIT_OUTPUT_DIR` / `JEST_JUNIT_OUTPUT_NAME` env contract (the+// `frontend.yml` job sets both). So the frontend "collector" is a post-run read+// of that one file. The XML parsing itself is shared (`../junit.ts`); this owns+// the frontend-specific parts — where jest-junit drops its file — while the+// suite label is supplied by the entrypoint.+//+// Two jest-junit details that mean there's nothing to massage here:+// - it already strips ANSI colour codes from failure bodies, so messages+// arrive plain (unlike a raw jest console dump);+// - it entity-escapes (rather than CDATA-wraps) those bodies, which+// `parseJunit`'s `elementBody` already decodes.+//+// FILE / IDENTITY NOTE: `jest.config.js` sets jest-junit's `addFileAttribute`,+// so each <testcase> carries the source path as a `file` attribute, which the+// shared parser surfaces as `file_path`. We deliberately leave jest-junit's+// `classname`/`name` on their defaults (the same `{ancestors} {title}` blob in+// both) rather than re-template them: that artifact is also what Trunk ingests,+// and changing those fields would re-baseline its test identity. So `test_path`+// and `test_name` carry that blob (redundant but a stable, unique key) and+// identity is (test_suite, test_path, test_name, file_path).++import { readFileSync } from "node:fs";++import type { NormalizedTest } from "../contract.ts";+import { findJunitFiles, parseJunit } from "../junit.ts";+import { log } from "../util.ts";++const JUNIT_DIR = process.env.JEST_JUNIT_OUTPUT_DIR || "target/junit";+const JUNIT_NAME = process.env.JEST_JUNIT_OUTPUT_NAME || "junit.xml";++/**+ * Pick jest-junit's output from a directory listing by the exact name its env+ * contract sets (`JEST_JUNIT_OUTPUT_NAME`). An empty match means nothing to+ * report.+ */+function selectJestJunit(name: string): (entries: string[]) => string[] {+ return (entries) => entries.filter((entry) => entry.endsWith(name));+}++/**+ * Normalize the JUnit file(s) jest-junit wrote under `dir` into+ * `NormalizedTest[]` — the source-specific half of the adapter pattern+ * (jest-junit XML → the shared normalized shape).+ */+export function normalizeFrontendJunit(+ dir: string = JUNIT_DIR,+ name: string = JUNIT_NAME,+): NormalizedTest[] {+ const files = findJunitFiles(dir, selectJestJunit(name));+ const failures = files.flatMap((file) => {+ try {+ return parseJunit(readFileSync(file, "utf8"));+ } catch (error) {+ console.error(`[ci-conductor] failed to read ${file}`, error);+ return [];+ }+ });+ log(+ `scanned ${dir}: ${files.length} JUnit file(s), ${failures.length} failing test(s)`,+ );+ for (const test of failures) {+ log(` failing: ${test.path || "(no describe path)"} / ${test.name}`);+ }+ return failures;+}diff --git a/release/ci-conductor/src/junit.test.ts b/release/ci-conductor/src/junit.test.tsindex dc03ce1f3e8..8683606edbe 100644--- a/release/ci-conductor/src/junit.test.ts+++ b/release/ci-conductor/src/junit.test.ts@@ -118,6 +118,20 @@ java.lang.RuntimeException: boom</testcase></testsuite>`;+// jest-junit with `addFileAttribute: "true"` emits a `file` attribute holding+// the source path, plus an entity-escaped (non-CDATA) failure body. The parser+// must surface that path as `file` and decode the body.+const JEST_WITH_FILE = `<?xml version="1.0" encoding="UTF-8"?>+<testsuites name="jest tests" tests="1" failures="1">+<testsuite name="Button" failures="1" tests="1">+<testcase classname="Button when open renders > the label" name="Button when open renders > the label" time="0.012" file="frontend/src/metabase/components/Button/Button.unit.spec.tsx">+<failure>Error: expect(received).toBe(expected)++ at Button.unit.spec.tsx:42:18</failure>+</testcase>+</testsuite>+</testsuites>`;+describe("parseJunit", () => {it("prefers the failure `message` attribute over the body", () => {const [test] = parseJunit(FAILURE_WITH_MESSAGE);@@ -178,6 +192,22 @@ describe("parseJunit", () => {]);});+ it("surfaces the testcase `file` attribute as file_path (frontend/jest)", () => {+ const tests = parseJunit(JEST_WITH_FILE);+ expect(tests).toHaveLength(1);+ const [test] = tests;+ expect(test.file).toBe(+ "frontend/src/metabase/components/Button/Button.unit.spec.tsx",+ );+ // The unescaped `>` in the title survives decoding.+ expect(test.name).toBe("Button when open renders > the label");+ // The <failure> has no `message` attribute, so message is null; the body+ // text lands in `stack`.+ expect(test.message).toBeNull();+ expect(test.stack).toContain("Error: expect(received).toBe(expected)");+ expect(test.status).toBe("failure");+ });+it("returns [] for malformed XML without throwing", () => {expect(parseJunit("<not-valid")).toEqual([]);});diff --git a/release/ci-conductor/src/junit.ts b/release/ci-conductor/src/junit.tsindex 553c5763f63..3ea578973a5 100644--- a/release/ci-conductor/src/junit.ts+++ b/release/ci-conductor/src/junit.ts@@ -1,12 +1,35 @@-// The shared JUnit XML parser: a JUnit `<testcase>`/`<failure>` document →-// NormalizedTest[]. Format-level and producer-agnostic — both the backend-// (hawk) and the frontend (jest-junit) adapters build on it. The per-suite-// parts (where the XML lives, how fields map, the suite label) stay in-// `adapters/`. JUnit XML is simple, machine-generated markup, so we parse it-// ourselves rather than pull in a dependency.+// The shared JUnit utilities: discover a producer's JUnit files in a directory+// (`findJunitFiles`) and turn one file's `<testcase>`/`<failure>` document into+// NormalizedTest[] (`parseJunit`). Format-level and producer-agnostic — both+// the backend (hawk) and the frontend (jest-junit) adapters build on these. The+// per-suite parts (which entries are this producer's files, how fields map, the+// suite label) stay in `adapters/`. JUnit XML is simple, machine-generated+// markup, so we parse it ourselves rather than pull in a dependency.++import { readdirSync } from "node:fs";+import { join } from "node:path";import type { NormalizedTest } from "./contract.ts";+/**+ * List files under `dir` (recursively), keeping those `select` returns, as+ * paths joined to `dir`. Each adapter passes the topical part — which entries+ * are *its* JUnit files (hawk's `*_test.xml`, jest-junit's configured output+ * name, ...). Returns [] on any IO error — a missing artifact directory is a+ * no-op, not a failure (reporting is best-effort).+ */+export function findJunitFiles(+ dir: string,+ select: (entries: string[]) => string[],+): string[] {+ try {+ const entries = readdirSync(dir, { recursive: true }).map(String);+ return select(entries).map((entry) => join(dir, entry));+ } catch {+ return [];+ }+}+/** Decode the XML entities a JUnit producer can emit in attribute values. */function decodeEntities(value: string): string {return value@@ -79,6 +102,11 @@ export function parseJunit(xml: string): NormalizedTest[] {continue;}const namespace = (attr(attrs, "classname") || "").trim();+ // `file` is a standard (optional) JUnit testcase attribute. jest-junit+ // emits it as the source path when `addFileAttribute: "true"` is set+ // (the frontend path); hawk never writes one, so backend testcases parse+ // to null — backend identity stays (test_suite, test_path, test_name).+ const file = (attr(attrs, "file") || "").trim();const stack = problems.map((p) => elementBody(p[3]))@@ -96,10 +124,7 @@ export function parseJunit(xml: string): NormalizedTest[] {tests.push({name,path: namespace || undefined,- // file_path is unstable for backend tests (the stack locator varies by- // failure mode), so identity is (test_suite, test_path, test_name) and- // file is sent as null.- file: null,+ file: file || null,message,stack: stack || undefined,// JUnit only tells us a test failed/errored, never that it recovered,diff --git a/release/ci-conductor/src/report-frontend.ts b/release/ci-conductor/src/report-frontend.tsnew file mode 100644index 00000000000..5275f5cecf7--- /dev/null+++ b/release/ci-conductor/src/report-frontend.ts@@ -0,0 +1,29 @@+#!/usr/bin/env bun++// Frontend test-failure reporter entrypoint. Runs as a post-test CI step on the+// frontend (jest) path: normalize the JUnit jest-junit just wrote and report the+// failures to ci-conductor. Best-effort — reporting must never break a test run.+//+// Run directly with bun (no build step): bun src/report-frontend.ts++import { normalizeFrontendJunit } from "./adapters/frontend.ts";+import { reportTestFailures } from "./transport.ts";+import { log } from "./util.ts";++async function main(): Promise<void> {+ log("frontend test-failure reporter starting");+ const env = process.env;+ // `test_suite` is the suite-level identity discriminator. Frontend unit tests+ // are jest-sharded across parallel jobs, but each test file runs on exactly+ // one shard, so every shard reports a disjoint subset under the SAME stable+ // suite (`fe-tests-unit`, the job name, set via `CI_CONDUCTOR_TEST_SUITE`) —+ // the shard number is deliberately NOT part of the suite, or a test would re-key in+ // ci-conductor whenever sharding changes. Falls back to a generic `frontend`.+ const testSuite = env.CI_CONDUCTOR_TEST_SUITE || "frontend";+ await reportTestFailures(normalizeFrontendJunit(), testSuite);+}++main().catch((error) => {+ // Last line of defense: reporting must never fail the job.+ console.error("[ci-conductor] frontend reporting failed (ignored)", error);+});