After this lesson, you will be able to: Run the test suite automatically on every pull request, fail the build on any failure, and publish coverage reports.
Tests that only run on your machine protect only you. Continuous integration runs the whole suite on every push and pull request, so a regression blocks the merge instead of reaching production. This lesson wires unit, integration, and e2e tests into GitHub Actions with coverage reporting.
Local tests are easy to skip under deadline pressure, and they only reflect your machine's state. CI runs the suite in a clean, reproducible environment on every pull request, for every contributor, every time. A failed test turns the PR red and (with branch protection) blocks the merge. That is what makes 'all tests pass' a property of main rather than a hope. It also catches the classic 'works on my machine' bugs from uncommitted files or local-only dependencies.
Run on every push and PR. Cache dependencies for speed. Fail the job (and the PR) on any test failure.
# .github/workflows/test.ymlname: testson:push: { branches: [main] }pull_request:jobs:test:runs-on: ubuntu-latestservices:postgres:image: postgres:16env: { POSTGRES_PASSWORD: test }ports: ['5432:5432']options: >---health-cmd pg_isready --health-interval 10s--health-timeout 5s --health-retries 5steps:- uses: actions/checkout@v4- uses: actions/setup-node@v4with: { node-version: 20, cache: npm }- run: npm ci- run: npm run test:ci # vitest run --coverage; non-zero exit fails the job- uses: codecov/codecov-action@v4with: { files: ./coverage/coverage-final.json }
A red CI run only matters if it stops the merge. In the GitHub repo settings, protect the main branch and mark the test job a required status check. Now a pull request cannot merge until tests pass, which is the entire point of CI. Add the e2e job as a separate workflow step (with Playwright's browsers cached) so slow e2e tests do not block the fast unit feedback, but still gate the merge.
Pick the best answer.
Sign in and purchase access to unlock this lesson.