All jobs

Add Code Coverage to CI for an Electron App

Yes. An agent turns on Jest coverage for an Electron app's main and renderer code, publishes the report from the GitHub Actions matrix, and opens a pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the test setup before it changes it. In an app built on electron-react-boilerplate, package.json runs jest as the test script, pins Node 24 and npm 10 through devEngines, and lists jest 30, jest-environment-jsdom, @testing-library/react and @testing-library/jest-dom as dev dependencies, while ts-jest and TypeScript live in a separate .erb/tooling/package.json that postinstall installs with npm --prefix .erb/tooling ci. jest.config.js resolves ts-jest through that tooling directory with createRequire, uses the jsdom environment, maps CSS to identity-obj-proxy and images to .erb/mocks, and runs .erb/scripts/check-build-exists.ts as a setup file, which throws unless release/app/dist/main/main.js and release/app/dist/renderer/renderer.js exist. That last point decides the workflow: tests cannot run before npm run build.

The tests that exist are src/__tests__/App.test.tsx, which renders App and checks the heading and two links, and src/__tests__/updates.test.ts, which mocks electron, electron-updater and electron-log under the node environment and checks that startAutoUpdates does nothing in development and initialises once when packaged. The code they cover sits in src/renderer/App.tsx and src/main/updates.ts; src/main/main.ts, src/main/menu.ts, src/main/preload.ts and src/main/util.ts have no tests, and .gitignore already ignores a coverage directory. .github/workflows/test.yml runs on push to main and on pull requests across macOS, Windows and Ubuntu, does npm ci, then npm run package, npm run lint, npm exec tsc -- --noEmit and npm test in one step, and finishes with the smoke test under xvfb-run on Linux.

The agent adds coverage without a new dependency, because Jest 30 ships its own instrumentation. In jest.config.js it sets collectCoverageFrom to src/**/*.{ts,tsx} minus src/__tests__, src/renderer/index.tsx and the preload.d.ts declaration, coverageDirectory to coverage, and coverageReporters to text-summary, lcov and json-summary, and adds coverageProvider: 'v8' so ts-jest's source maps are read without Babel instrumentation. It adds a test:coverage script, jest --coverage, and leaves npm test unchanged for local runs. Because collectCoverageFrom counts files no test imports, src/main/main.ts and src/main/menu.ts appear at zero, which is the honest number and the reason to add the step.

In test.yml the agent splits the combined step so npm test becomes npm run test:coverage after npm run package has built the bundles the setup file checks for, uploads coverage/lcov.info and the HTML report with actions/upload-artifact under a name that includes matrix.os, and uploads lcov.info with codecov/codecov-action@v5 using a CODECOV_TOKEN secret and fail_ci_if_error: false so a fork's pull request still passes. It adds one small test, src/__tests__/util.test.ts, for resolveHtmlPath, which returns the dev server URL when ELECTRON_RENDERER_URL is set and the app is not packaged and a file: URL otherwise, so the first coverage report shows the main-process files are reachable. It runs npm ci, npm run package and npm run test:coverage in the sandbox and pastes the text-summary block in the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "codex",
  "input": "Add code coverage to the Jest tests and the GitHub Actions workflow of this Electron app and open a pull request. Read package.json, .erb/tooling/package.json, jest.config.js, .erb/scripts/check-build-exists.ts, .erb/scripts/smoke-test.cjs, electron.vite.config.ts, .gitignore, .github/workflows/test.yml, src/__tests__/App.test.tsx, src/__tests__/updates.test.ts, src/main/main.ts, src/main/util.ts, src/main/updates.ts, src/main/preload.ts and src/renderer/App.tsx first. Use Jest's built-in coverage with the v8 provider and no new dependency: set collectCoverageFrom to the TypeScript under src excluding __tests__, src/renderer/index.tsx and declaration files, coverageDirectory coverage, and text-summary, lcov and json-summary reporters. Add a test:coverage script that runs jest --coverage and keep npm test as it is. In test.yml, run npm run test:coverage after npm run package in the existing matrix, upload the coverage directory as a workflow artifact named per OS, and upload lcov.info with codecov/codecov-action@v5 using a CODECOV_TOKEN secret with fail_ci_if_error false. Add src/__tests__/util.test.ts covering resolveHtmlPath in development and packaged mode. Do not set a coverage threshold. Run npm ci, npm run package, npm run lint, npm exec tsc -- --noEmit and npm run test:coverage, and paste the text-summary block in the pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch that changes jest.config.js, package.json and .github/workflows/test.yml and adds src/__tests__/util.test.ts. The package-lock.json is untouched because no dependency is added, which a reviewer can verify from the file list. The description holds the text-summary block from the sandbox run, with statement, branch, function and line percentages for the files under src, and names the files that report zero so the number is read as a starting point rather than a result.

The run's full record at GET /o/{org}/sessions/{id}/messages keeps the npm run package output that produced the bundles the Jest setup file requires, the lint and type-check output, and the coverage run itself. GET /o/{org}/p/{project}/sessions/{id}/changes returns the diff per file. The workflow change is visible on the pull request's own checks once it is pushed, where each OS in the matrix uploads its artifact.

Before merging, a reviewer checks that collectCoverageFrom leaves out the generated release/app/dist and .erb directories, that the coverage step still runs after npm run package on all three operating systems, and that the Codecov upload cannot fail the check for contributors without the secret. No threshold is set in this change, so a later pull request can add one once the team agrees on the number.

Run it on every Electron repository without a coverage number

A team that wants the same report on every desktop app sends one POST /o/{org}/sessions per repository with this prompt and the repository in resources. Each run reads its own jest.config.js, so an app on Vitest gets @vitest/coverage-v8 instead, an app with a Babel transform gets the babel provider, and an app whose tests do not need a prior build gets the step placed before packaging. Each run opens its own pull request.

Set a webhook_url on the API key and Rush POSTs run.completed when each turn ends, with session_url to read the pull request and the pasted summary from. A request_id per repository returns the run already started if the batch script is run again.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

The catch in an Electron React Boilerplate app is the setup file that refuses to run tests until the app is built, so coverage has to be wired into a workflow that already packages the app on three operating systems. Getting that order right on a laptop means waiting on three CI runs per attempt. The agent runs the package step and the coverage run in its own sandbox first and pushes a workflow it has already seen work.

The result is a pull request with the summary pasted in and a trajectory that shows the commands behind it. A reviewer compares the numbers in the description with the Codecov comment that lands on the pull request after the first CI run, and the two agree because they came from the same config.

Frequently asked questions

Why the v8 provider rather than Babel instrumentation?

The tests are compiled by ts-jest from the isolated .erb/tooling install, and the babel provider would add babel-plugin-istanbul and a transform step that the repository does not otherwise use. The v8 provider reads coverage from the engine and maps it back through ts-jest's source maps, so the report names src/main/updates.ts rather than compiled output, with nothing new installed.

Will the main process files show real numbers?

Only where tests import them. src/main/updates.ts and src/main/util.ts are covered after this change; src/main/main.ts and src/main/menu.ts are listed by collectCoverageFrom at zero because they create windows and menus at import time and need an Electron process to run. The report makes that gap visible rather than hiding it, and a Playwright run against the packaged app is the way to cover them.

Does it slow the matrix down?

The coverage run replaces the npm test call in the same step, so the matrix runs Jest once per OS as before, with instrumentation on. The artifact upload adds one step per job. The Codecov upload runs with fail_ci_if_error: false, so a slow or unreachable upload never turns a green run red.

Can it enforce a minimum percentage?

Yes, through coverageThreshold in jest.config.js, but this change leaves it out on purpose: the first report is the number to argue from. Once the team picks a figure, a one-line follow-up adds the threshold and the same workflow fails below it. Ask for a threshold in the prompt and the agent sets it from the summary it measured.

More Electron jobs

The same job on other stacks

Keep reading