In this tutorial, we’ll refactor the Unit Test stage of a Jenkins pipeline to run tests across multiple Node.js versions—18, 19, and 20—using Kubernetes agents and Docker containers. This approach improves reliability and ensures compatibility before merging changes.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.
Table of Contents
- Pipeline Configuration with Kubernetes Agent
- Sequential Unit Testing and Coverage
- Parallel Testing Across Node.js Versions
- Diagnosing the Node.js 20 Failure
- Installing Dependencies in Node.js 20 Stage
- Next Steps and Further Reading
Pipeline Configuration with Kubernetes Agent
Below is an excerpt from the originalJenkinsfile that configures a Kubernetes agent, Node.js tool, and necessary environment variables:
Adjust
yamlFile and defaultContainer to match your Kubernetes Pod specification.Sequential Unit Testing and Coverage
Originally, the pipeline ran Unit Testing and Code Coverage sequentially on Node.js 18:Parallel Testing Across Node.js Versions
To ensure your application works on Node.js 18, 19, and 20, we can run tests in parallel branches. This refactoring includes:- Installing dependencies once on the Kubernetes agent
- Running dependency scanning (e.g., Snyk, Trivy)
- Testing in parallel across multiple Node.js versions
- Aggregating code coverage
Pipeline Snippet
Agent and Container Matrix
| Stage | Environment | Container/Agent | Purpose |
|---|---|---|---|
| Dependencies | Kubernetes Pod | node-18 | Install node_modules via npm install |
| Scanning | Kubernetes Pod | node-18 | Security and license scanning |
| NodeJS 18 | Kubernetes Pod | node-18 | Default Node.js |
| NodeJS 19 | Kubernetes Pod | node-19 container | Verify compatibility with v19 |
| NodeJS 20 | Docker-in-Docker Pod | node:20-alpine Docker | Validate on latest LTS |
| Coverage | Kubernetes Pod | node-18 | Generate and publish code coverage |
Diagnosing the Node.js 20 Failure
After pushing the refactoredJenkinsfile, the NodeJS 20 branch failed:
The Docker container
node:20-alpine starts with a clean filesystem—it doesn’t have your node_modules. Always install dependencies inside each container or agent before running tests.Installing Dependencies in Node.js 20 Stage
Option 1: Inline npm install
Add npm install directly before running tests in the NodeJS 20 branch:
Option 2: Separate Install Step in Parallel Stage
Alternatively, include installation as part of the Unit Testing parallel stage:node_modules prior to execution.
Next Steps and Further Reading
- Explore adding test result archiving with
junitplugin. - Integrate SonarQube for code quality reporting.
- Consider dynamic matrix stages for other runtimes (e.g., Node.js 21+).
Links and References
- Jenkins Kubernetes Plugin
- Node.js Docker Official Images
- Jenkins Pipeline Syntax
- Mocha Testing Framework
That completes the refactoring of the Unit Test stage to support parallel testing across multiple Node.js versions.