Skip to content

Add CI workflow with Node.js matrix testing and package manager detection#1

Closed
Copilot wants to merge 4 commits into
mainfrom
copilot/add-ci-workflow-for-tests
Closed

Add CI workflow with Node.js matrix testing and package manager detection#1
Copilot wants to merge 4 commits into
mainfrom
copilot/add-ci-workflow-for-tests

Conversation

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor

Adds a production-ready CI workflow that runs tests across Node.js 18.x and 20.x with automatic package manager detection and dependency caching.

Implementation

  • Matrix testing: Runs on Node.js 18.x and 20.x in parallel
  • Package manager detection: Runtime detection of npm/yarn/pnpm via lockfile presence, with appropriate install commands (npm ci, yarn install --frozen-lockfile, pnpm install --frozen-lockfile)
  • Dependency caching: Leverages actions/setup-node@v4 built-in cache using detected manager and lockfile path
  • Triggers: Push to main/develop, PRs targeting main/develop, manual dispatch via workflow_dispatch
  • Security: Explicit permissions: contents: read following least privilege

Workflow Structure

jobs:
  test:
    strategy:
      matrix:
        node-version: [18.x, 20.x]
    permissions:
      contents: read
    steps:
      - Detect package manager and lockfile
      - Setup Node.js with caching
      - Install with frozen lockfile
      - Run tests with CI=true

The workflow respects the existing test script configuration in package.json (already includes --watchAll=false --passWithNoTests) rather than overriding with duplicate flags.

Original prompt

Add a GitHub Actions workflow to run tests for the Node/React app.

Summary:

  • Create a new branch and open a pull request that adds a CI workflow file at .github/workflows/ci-tests.yml.
  • The workflow should run on pushes to main/develop, on pull requests, and be triggerable manually.
  • It must test on Node.js 18.x and 20.x (matrix), detect package manager (npm/yarn/pnpm), cache deps using setup-node, install dependencies using the detected package manager and lockfile, and run tests once (non-watch) with CI=true.

Files to add:

  • .github/workflows/ci-tests.yml

Contents of .github/workflows/ci-tests.yml:

# GitHub Actions workflow: CI - run tests for Node/React app
# Triggers on push to main/develop and on pull requests. Detects package manager (npm/yarn/pnpm),
# caches dependencies, installs, and runs tests once (non-watch).
name: CI - Tests

on:
  push:
    branches: [ "main", "develop" ]
  pull_request:
    branches: [ "main", "develop" ]
  workflow_dispatch:

jobs:
  test:
    name: Test on Node.js ${{ matrix.node-version }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x, 20.x]

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Detect package manager and lockfile
        id: detect
        shell: bash
        run: |
          # default to npm if nothing is found
          if [ -f pnpm-lock.yaml ]; then
            echo "manager=pnpm" >> $GITHUB_OUTPUT
            echo "lockfile=pnpm-lock.yaml" >> $GITHUB_OUTPUT
            echo "install=pnpm install --frozen-lockfile" >> $GITHUB_OUTPUT
          elif [ -f yarn.lock ]; then
            echo "manager=yarn" >> $GITHUB_OUTPUT
            echo "lockfile=yarn.lock" >> $GITHUB_OUTPUT
            echo "install=yarn install --frozen-lockfile" >> $GITHUB_OUTPUT
          elif [ -f package-lock.json ]; then
            echo "manager=npm" >> $GITHUB_OUTPUT
            echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
            echo "install=npm ci" >> $GITHUB_OUTPUT
          else
            # fallback
            echo "manager=npm" >> $GITHUB_OUTPUT
            echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
            echo "install=npm install" >> $GITHUB_OUTPUT
          fi

      - name: Setup Node.js and dependency cache
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: ${{ steps.detect.outputs.manager }}
          cache-dependency-path: ${{ steps.detect.outputs.lockfile }}

      - name: Install dependencies
        shell: bash
        run: |
          echo "Using package manager: ${{ steps.detect.outputs.manager }}"
          echo "Running install command: ${{ steps.detect.outputs.install }}"
          eval "${{ steps.detect.outputs.install }}"

      - name: Run tests
        env:
          CI: true
        shell: bash
        run: |
          echo "Running test script using ${{ steps.detect.outputs.manager }}"
          if [ "${{ steps.detect.outputs.manager }}" = "npm" ]; then
            npm test -- --watchAll=false
          elif [ "${{ steps.detect.outputs.manager }}" = "yarn" ]; then
            yarn test --watchAll=false
          else
            pnpm test -- --watchAll=false
          fi

Notes/assumptions:

  • I did not set a base branch explicitly because the user did not specify one; the PR will target the repository default branch.
  • Branch name suggestion: ci/add-test-workflow (the implementation may create this branch name).

Please create the branch, add the file, and open a pull request with a descriptive title and PR description that explains the change and how to test it.

This pull request was created as a result of the following prompt from Copilot chat.

Add a GitHub Actions workflow to run tests for the Node/React app.

Summary:

  • Create a new branch and open a pull request that adds a CI workflow file at .github/workflows/ci-tests.yml.
  • The workflow should run on pushes to main/develop, on pull requests, and be triggerable manually.
  • It must test on Node.js 18.x and 20.x (matrix), detect package manager (npm/yarn/pnpm), cache deps using setup-node, install dependencies using the detected package manager and lockfile, and run tests once (non-watch) with CI=true.

Files to add:

  • .github/workflows/ci-tests.yml

Contents of .github/workflows/ci-tests.yml:

# GitHub Actions workflow: CI - run tests for Node/React app
# Triggers on push to main/develop and on pull requests. Detects package manager (npm/yarn/pnpm),
# caches dependencies, installs, and runs tests once (non-watch).
name: CI - Tests

on:
  push:
    branches: [ "main", "develop" ]
  pull_request:
    branches: [ "main", "develop" ]
  workflow_dispatch:

jobs:
  test:
    name: Test on Node.js ${{ matrix.node-version }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x, 20.x]

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Detect package manager and lockfile
        id: detect
        shell: bash
        run: |
          # default to npm if nothing is found
          if [ -f pnpm-lock.yaml ]; then
            echo "manager=pnpm" >> $GITHUB_OUTPUT
            echo "lockfile=pnpm-lock.yaml" >> $GITHUB_OUTPUT
            echo "install=pnpm install --frozen-lockfile" >> $GITHUB_OUTPUT
          elif [ -f yarn.lock ]; then
            echo "manager=yarn" >> $GITHUB_OUTPUT
            echo "lockfile=yarn.lock" >> $GITHUB_OUTPUT
            echo "install=yarn install --frozen-lockfile" >> $GITHUB_OUTPUT
          elif [ -f package-lock.json ]; then
            echo "manager=npm" >> $GITHUB_OUTPUT
            echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
            echo "install=npm ci" >> $GITHUB_OUTPUT
          else
            # fallback
            echo "manager=npm" >> $GITHUB_OUTPUT
            echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
            echo "install=npm install" >> $GITHUB_OUTPUT
          fi

      - name: Setup Node.js and dependency cache
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: ${{ steps.detect.outputs.manager }}
          cache-dependency-path: ${{ steps.detect.outputs.lockfile }}

      - name: Install dependencies
        shell: bash
        run: |
          echo "Using package manager: ${{ steps.detect.outputs.manager }}"
          echo "Running install command: ${{ steps.detect.outputs.install }}"
          eval "${{ steps.detect.outputs.install }}"

      - name: Run tests
        env:
          CI: true
        shell: bash
        run: |
          echo "Running test script using ${{ steps.detect.outputs.manager }}"
          if [ "${{ steps.detect.outputs.manager }}" = "npm" ]; then
            npm test -- --watchAll=false
          elif [ "${{ steps.detect.outputs.manager }}" = "yarn" ]; then
            yarn test --watchAll=false
          else
            pnpm test -- --watchAll=false
          fi

Notes/assumptions:

  • I did not set a base branch explicitly because the user did not specify one; the PR will target the repository default branch.
  • Branch name suggestion: ci/add-test-workflow (the implementation may create this branch name).

Please create the branch, add the file, and open a pull request with a descriptive title and PR description that explains the change and how to test it.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@netlify

netlify Bot commented Dec 13, 2025

Copy link
Copy Markdown

Deploy Preview for legendary-sunburst-82c066 failed. Why did it fail? →

Name Link
🔨 Latest commit 78355e5
🔍 Latest deploy log https://app.netlify.com/projects/legendary-sunburst-82c066/deploys/693d983910cdfb00087e3236

Copilot AI and others added 3 commits December 13, 2025 16:43
Co-authored-by: Andrew112 <12086319+Andrew112@users.noreply.github.com>
Co-authored-by: Andrew112 <12086319+Andrew112@users.noreply.github.com>
Co-authored-by: Andrew112 <12086319+Andrew112@users.noreply.github.com>
Copilot AI changed the title [WIP] Add GitHub Actions workflow for Node/React app tests Add CI workflow with Node.js matrix testing and package manager detection Dec 13, 2025
Copilot AI requested a review from Andrew112 December 13, 2025 16:48

@Andrew112 Andrew112 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a good attempt at deplying my website allthoguh netlify failed to launch.

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

this was a good attempt at deplying my website allthoguh netlify failed to launch.

This PR adds a CI workflow for running tests (ci-tests.yml), not deployment. The workflow only runs tests on Node.js 18.x and 20.x when code is pushed. Any Netlify deployment issues would be separate from this change - Netlify typically deploys via its own integration or a different workflow.

Copilot AI requested a review from Andrew112 December 13, 2025 17:21
Andrew112 added a commit that referenced this pull request Dec 16, 2025
@Andrew112 Andrew112 marked this pull request as ready for review December 22, 2025 01:53
@Andrew112 Andrew112 closed this Dec 22, 2025
@Andrew112 Andrew112 deleted the copilot/add-ci-workflow-for-tests branch December 22, 2025 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants