Writing a GitHub Actions File To Test Against Multiple Node.js Versions

If you’re distributing a package, say a plugin, you may want to test it against multiple Node versions (especially if you’re using Jest for tests). In my situation, I have a popular plugin for Aurelia called Aurelia Google Maps. It’s for Aurelia 1, but many people use it, so I wanted to test it against the LTS of Node and the latest version.

name: Node.js Jest Tests

on: [push, pull\_request]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'yarn'

    - name: Install dependencies
      run: yarn install

    - name: Run Jest tests
      run: yarn test

    strategy:
      matrix:
        node-version: [18.x, 19.x]

You will want to save this file in your .github/workflows directory at the root of your project. You can save it as whatever you want: run-tests.yml

As you can see in the on part, the tests will run on every push and pull request. It’s also worth going in and ensuring you have some rules to ensure only pull requests that pass the tests meet the criteria for merging too.