Project 2 โ€บ 02 Run tests on every pushGitHub โ†—
๐Ÿค– AI Prompt โ€” stuck? paste this

โ€œUpdate my workflow to set up Node.js 20, install deps with npm ci, and run npm test. Explain why npm ci is preferred over npm install in CI.โ€

02 โ€” Run your tests automatically on every push

Category: Workflows ยท Priority: High ยท Step: 3

This is the heart of CI: install dependencies and run the test suite on a clean machine.

๐ŸŽฏ What is this task?

A green "hello world" is nice, but real CI runs your tests. In this task the workflow installs your dependencies and runs npm test on a fresh ubuntu-latest machine โ€” exactly as a teammate would on a clean checkout. If a test fails, the run goes red and the pull request is blocked.

๐Ÿค– AI Prompt

"Update my GitHub Actions workflow to set up Node.js 20, install dependencies with npm ci, and run npm test. Explain why npm ci is preferred over npm install in CI."

๐Ÿ“‹ Step-by-step

Update .github/workflows/ci.yml:

name: CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

Why npm ci and not npm install? npm ci installs the exact versions from package-lock.json and fails if the lockfile is out of sync. It's faster and reproducible โ€” ideal for CI.

โœ… Done when

  • The workflow runs npm ci then npm test
  • A passing test suite makes the run green
  • Deliberately breaking a test turns the run red (then fix it)

๐Ÿ“ Quiz

Answer all 2 questions correctly to complete this lesson.

1. Why use `npm ci` instead of `npm install` in CI?
2. What happens to a pull request if a test fails in CI?
โ† PreviousPass the quiz to completeNext โ†’