How to run my tests before deploying my app

Hi, I am using Qovery to build and deploy my application, and I’d like to run my tests before deploying my application. Is it possible? How to do it?

Thank you.

Hi, it’s a good question, and I assume you have connected your application via Git and by providing a Dockerfile, right? In this case, you can add a stage to your Dockefile to run your tests. Here is an example with a simple Dockerfile for a NodeJS app.

My original Dockerfile

# Define the base image
FROM node:14

# Set the working directory in the Docker container
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# If you are building your code for production
# RUN npm ci --only=production

# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .

# Expose the port that your app runs in
EXPOSE 8080

# The command to run your application
CMD [ "node", "app.js" ]

Now let’s turn this Dockerfile into a Dockerfile with an additional testing stage

# Define the base image
FROM node:14 AS build

# Set the working directory in the Docker container
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .

# Run tests
FROM build AS test
RUN npm test

# Production build
FROM build AS production

# If you are building your code for production
# RUN npm ci --only=production

# Expose the port that your app runs in
EXPOSE 8080

# The command to run your application
CMD [ "node", "app.js" ]

In this Dockerfile:

  • The build stage is where the application’s dependencies are installed, and the source code is copied.
  • The test stage runs the npm test command to execute your tests. If the tests fail, the Docker image build will stop at this stage and won’t proceed to the next stage. → If the tests fail, you’ll see what happened from the Qovery deployment logs console.
  • The production stage will only be built if the test stage completes successfully. The application is prepared to be run in a production environment at this stage.

I hope it helps.

Hi,

Another option is to use a standard CI (GitHub Action or Gitlab CI for example), run all your tests on it, then use the Qovery GitHub action at the end How to integrate Qovery with GitHub Actions | Qovery