How to automate CI/CD pipeline unit tests for a node.js container deployment in kubernetes

Viewed 28

I am trying to automate unit testing before deploying a node.js container to a local kubernetes cluster. It is not clear to me whether or not I need to configure this in my deployment.yaml, Dockerfile, package.json, or some combination of them. And once configured how to instruct Kubernetes to output any failures and exit before deploying.

Do I need to do as this SO answer suggests and write a shell script and modify environment variables? Or, is this something I can automate with Kubernetes deployment.yaml

If it's useful, I am using mocha with chai. And this is being deployed from Google Cloud Source Repositories to a local Kubernetes instance.

Since I'm entirely new to Kubernetes, it would be great to have as much detail as possible.

Here is my deployment.yaml:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app-container
          image: my-app-image
          imagePullPolicy: IfNotPresent

Here is my Dockerfile:

# Use base node 18-alpine image from Docker hub
FROM node:18-alpine

WORKDIR /MY_APP

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy rest of the application source code
COPY . .

# Run index.js
ENTRYPOINT ["node", "src/index.js"]

Here is my package.json:

  "scripts": {
    "start": "node src/index.js",
    "test": "npm install mocha -g && mocha --timeout 4000 --exit"
   }

And here is a basic unit test I'm using to experiment with:

import { expect } from 'chai'

describe('Basic unit test', ()=>{
    it('Checks if 3*3=9', () => {

        expect(3*3, 9)
    })
})
0 Answers
Related