<p>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 <code>deployment.yaml</code>, <code>Dockerfile</code>, <code>package.json</code>, or some combination of them. And once configured how to instruct Kubernetes to output any failures and exit before deploying.</p>
<p>Do I need to do as <a href="https://stackoverflow.com/a/64655551/1551027">this SO answer</a> suggests and write a shell script and modify environment variables? Or, is this something I can automate with Kubernetes <code>deployment.yaml</code></p>
<p>If it's useful, I am using <code>mocha</code> with <code>chai</code>. And this is being deployed from Google Cloud Source Repositories to a local Kubernetes instance.</p>
<p>Since I'm entirely new to Kubernetes, it would be great to have as much detail as possible.</p>
<p>Here is my <code>deployment.yaml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>
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
</code></pre>
<p>Here is my <code>Dockerfile</code>:</p>
<pre><code># 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 [&quot;node&quot;, &quot;src/index.js&quot;]
</code></pre>
<p>Here is my <code>package.json</code>:</p>
<pre class="lang-js prettyprint-override"><code>  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;node src/index.js&quot;,
    &quot;test&quot;: &quot;npm install mocha -g &amp;&amp; mocha --timeout 4000 --exit&quot;
   }
</code></pre>
<p>And here is a basic unit test I'm using to experiment with:</p>
<pre class="lang-js prettyprint-override"><code>import { expect } from 'chai'

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

        expect(3*3, 9)
    })
})
</code></pre>

Dshiz
<p>My issue was a misunderstanding about how the CI/CD pipeline works when it comes to tests.</p>
<p>It goes like this:
<code>Commit &gt; Build (dev) &gt; Deploy (dev) &gt; Run tests &gt; (pass or fail) &gt; (if pass) promote to production commit &gt; ... deploy</code>.</p>
<p>If the dev deploy fails the tests, the code is never committed, therefore never moves forward in the pipeline.</p>
<p>In my case, my <code>package.json</code> file needed this:</p>
<pre class="lang-json prettyprint-override"><code>  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;node src/index.js&quot;,
    &quot;test&quot;: &quot;npm install mocha -g &amp;&amp; mocha --timeout 4000 --exit&quot;,
    &quot;build&quot;: &quot;docker image build -f ./Dockerfile -t my_project:dev .&quot;
  },
</code></pre>
<p>My <code>cloudbuild.json</code> file needed this:</p>
<pre class="lang-yaml prettyprint-override"><code>- id: &quot;Run unit tests&quot;
  name: node
  entrypoint: npm
  args: [&quot;test&quot;]
- id: &quot;run&quot;
  name: node
  entrypoint: npm
  args: [&quot;start&quot;]
</code></pre>
<p>If the tests pass, the deploy proceeds, otherwise the build fails.</p>
