CI Pipeline with Automated Tests
What you'll build
A complete CI pipeline configuration for GitHub Actions that installs dependencies, runs tests with coverage, and fails the build if tests fail or coverage drops below thresholds.
Goals
- Create a GitHub Actions workflow file
- Run tests automatically on push and pull request
- Cache dependencies for faster CI runs
- Upload coverage reports as artifacts
- Understand CI test gates and failure handling
Prerequisites
- A GitHub repository with tests
- Completed Project 1 or equivalent test suite
- Basic understanding of YAML
Steps
- 1
Create the workflow file
Create
.github/workflows/test.ymlto define the CI pipeline.Codename: Tests on: push: branches: [ main, develop ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm' - run: npm ci - run: npm test -- --coverage - uses: actions/upload-artifact@v4 with: name: coverage-report path: coverage/Expected result
Workflow file is created.
- 2
Add coverage threshold enforcement
Configure Jest to fail if coverage drops below thresholds in package.json.
Code"jest": { "coverageThreshold": { "global": { "branches": 80, "functions": 80, "lines": 80, "statements": 80 } } }Expected result
CI will fail if coverage is below 80%.
- 3
Test the pipeline locally
Run the same commands locally to verify they work:
npm ci && npm test -- --coverage.Expected result
Local run succeeds, matching what CI will do.
- 4
Push and verify CI runs
Commit and push to trigger the CI pipeline. Watch the Actions tab on GitHub to see the pipeline execute.
Expected result
CI pipeline runs and passes on GitHub.
Hint
If CI fails, read the logs to understand what went wrong.
- 5
Add parallel test execution
Optimize CI by running tests in parallel.
Code- run: npm test -- --maxWorkers=2 --coverageExpected result
CI tests run faster with parallel workers.
Try it yourself
What you learned
You learned to set up a CI pipeline that automatically runs tests on every push and pull request. CI acts as a quality gate — failing tests or insufficient coverage blocks the merge. This ensures code quality is maintained consistently across the team.
Related Resources
Related Lessons
- CI Testing
Running tests in CI pipelines.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.