Tests workflow (mochajs/mocha)
The Tests workflow from mochajs/mocha, explained and optimized by Latchkey.
CI health: C - fair
Point runs-on at Latchkey and get run de-duplication, job timeouts, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the Tests workflow from the mochajs/mocha repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
name: Tests
on:
push:
branches:
- main
paths-ignore:
["*.md", "docs/**", ".github/**", ".prettierignore", "AUTHORS", "LICENSE"]
pull_request:
branches:
- main
paths-ignore:
["*.md", "docs/**", ".github/**", ".prettierignore", "AUTHORS", "LICENSE"]
types: [opened, synchronize, reopened, edited]
workflow_dispatch:
permissions:
contents: read
id-token: write # for Codecov OIDC
jobs:
lint:
uses: ./.github/workflows/npm-script.yml
with:
npm-script: lint
smoke:
uses: ./.github/workflows/npm-script.yml
with:
node-versions: "20,22,24"
npm-script: test-smoke
test-node-lts:
# TODO: Restore "mocha-github-actions-reporter" style reporting without relying on third party module
# https://github.com/mochajs/mocha/issues/5183
uses: ./.github/workflows/npm-script.yml
needs: smoke
strategy:
fail-fast: false
matrix:
test-part:
- interfaces
- unit
- integration
- jsapi
- requires
- reporters
- only
with:
npm-script: test-node:${{ matrix.test-part }}
test-node-all:
name: Test ${{ matrix.test-part }} in all environments
permissions:
id-token: write # for Codecov OIDC
contents: read # for Codecov OIDC
# TODO: Restore "mocha-github-actions-reporter" style reporting without relying on third party module
# https://github.com/mochajs/mocha/issues/5183
uses: ./.github/workflows/npm-script.yml
needs: test-node-lts
strategy:
fail-fast: false
matrix:
coverage: [true]
test-part:
- interfaces
- unit
- integration
- requires
- reporters
- only
include:
- test-part: jsapi
coverage: false
with:
os: "ubuntu-latest,windows-latest"
# We pin exact versions here per https://github.com/mochajs/mocha/issues/5052
# Ref https://nodejs.org/en/about/previous-releases
node-versions: "20.19.4,22.18.0,24.6.0"
npm-script: test-node:${{ matrix.test-part }}
coverage: ${{ matrix.coverage }}
test-browser:
name: Browser tests
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
- uses: actions/setup-node@v6
with:
node-version: "22"
cache: "npm"
- run: npm ci --ignore-scripts
- run: npm run test-browser
env:
NODE_OPTIONS: "--trace-warnings"
tsc:
uses: ./.github/workflows/npm-script.yml
with:
npm-script: tsc
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
name: Tests on: push: branches: - main paths-ignore: ["*.md", "docs/**", ".github/**", ".prettierignore", "AUTHORS", "LICENSE"] pull_request: branches: - main paths-ignore: ["*.md", "docs/**", ".github/**", ".prettierignore", "AUTHORS", "LICENSE"] types: [opened, synchronize, reopened, edited] workflow_dispatch: permissions: contents: read id-token: write # for Codecov OIDC concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: lint: timeout-minutes: 30 uses: ./.github/workflows/npm-script.yml with: npm-script: lint smoke: timeout-minutes: 30 uses: ./.github/workflows/npm-script.yml with: node-versions: "20,22,24" npm-script: test-smoke test-node-lts: timeout-minutes: 30 # TODO: Restore "mocha-github-actions-reporter" style reporting without relying on third party module # https://github.com/mochajs/mocha/issues/5183 uses: ./.github/workflows/npm-script.yml needs: smoke strategy: fail-fast: false matrix: test-part: - interfaces - unit - integration - jsapi - requires - reporters - only with: npm-script: test-node:${{ matrix.test-part }} test-node-all: timeout-minutes: 30 name: Test ${{ matrix.test-part }} in all environments permissions: id-token: write # for Codecov OIDC contents: read # for Codecov OIDC # TODO: Restore "mocha-github-actions-reporter" style reporting without relying on third party module # https://github.com/mochajs/mocha/issues/5183 uses: ./.github/workflows/npm-script.yml needs: test-node-lts strategy: fail-fast: false matrix: coverage: [true] test-part: - interfaces - unit - integration - requires - reporters - only include: - test-part: jsapi coverage: false with: os: "ubuntu-latest,windows-latest" # We pin exact versions here per https://github.com/mochajs/mocha/issues/5052 # Ref https://nodejs.org/en/about/previous-releases node-versions: "20.19.4,22.18.0,24.6.0" npm-script: test-node:${{ matrix.test-part }} coverage: ${{ matrix.coverage }} test-browser: name: Browser tests runs-on: latchkey-small timeout-minutes: 20 permissions: contents: read steps: - uses: actions/checkout@v6 with: persist-credentials: false - uses: actions/setup-node@v6 with: node-version: "22" cache: "npm" - run: npm ci --ignore-scripts - run: npm run test-browser env: NODE_OPTIONS: "--trace-warnings" tsc: timeout-minutes: 30 uses: ./.github/workflows/npm-script.yml with: npm-script: tsc
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Add a job timeout so a hung step cannot burn hours of runner time.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Dependency installs
This workflow runs 6 jobs (17 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.