Leverage GitHub Actions’ matrix strategy to run your unit tests across multiple Node.js versions and operating systems in parallel. This ensures comprehensive coverage and compatibility without duplicating workflows.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Why Use a Matrix Strategy?
- Parallel execution for faster feedback
- Broad runtime and OS compatibility
- Simplified maintenance by centralizing variations
1. Defining the Matrix
First, configure your workflow triggers, environment variables, and a basic matrix under theunit-testing job. Here’s an example that hard-codes the runner and Node.js version:
We define three Node.js versions and two OS targets. The
exclude block omits Node.js 18 on macOS.Matrix Combinations
| Node.js Version | Operating System | Included? |
|---|---|---|
| 18 | ubuntu-latest | ✔️ |
| 18 | macos-latest | ❌ |
| 19 | ubuntu-latest | ✔️ |
| 19 | macos-latest | ✔️ |
| 20 | ubuntu-latest | ✔️ |
| 20 | macos-latest | ✔️ |
2. Referencing Matrix Variables
Next, updateruns-on and the Node.js setup step to dynamically reference your matrix values. This allows every job to automatically select the correct OS and Node.js version:
Use
${{ matrix.nodejs_version }} and ${{ matrix.operating_system }} to automatically pick each configured combination.3. Running and Observing the Workflow
Commit and push your changes with a descriptive message, for example:
