Skip to main content
Version: 2.0.0

Express-Mongo

A simple sample CRUD application to test using Keploy build with Express and MongoDB.

Setup application

Clone the repository and move to express-mongo folder

git clone https://github.com/keploy/samples-typescript && cd samples-typescript/express-mongo

# Install the dependencies
npm install

Using Keploy :

There are two ways to use Keploy:-

  1. Natively on Linux/WSL
  2. Using Docker

Natively on Ubuntu/WSL

Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system architecture, install the keploy latest binary release from here:-

Linux

  1. AMD Architecture
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
2. ARM Architecture
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy

Windows Subsystem for Linux (WSL)

On Windows, WSL is required to run Keploy Binary. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 to use the commands below.

wsl --install

Once installed download and Install "Keploy Binary" :

curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy

Let's start the MongoDB Instance

docker-compose up -d

Since we have setup our sample-app natively, we need to update the mongoDB host on line 41, in db/connection.js, from mongodb://mongoDb:27017/keploy to mongodb://127.0.0.1:27017/keploy.

Capture the testcases

sudo -E env PATH=$PATH Keploy record -c 'npm run src/app.js'

Let's generate the testcases.

Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

curl --request POST \
--url http://localhost:8000/students \
   --header 'content-type: application/json' \
   --data '{
    "name":"John Do",
    "email":"john@xyiz.com",
    "phone":"0123456799"
    }'

we will get the output:

Student registration successful!

We will get the following output in our terminal

Testcase

Running the testcases

sudo -E env PATH=$PATH keploy test -c 'npm run src/app.js' --delay 10

Our testcases will fail as the Keep-Alive connection won't be available when we are using testmode, this happen because in test mode the Keploy uses the Mocks.yml, which was generated in the record mode.

Testcase

Let's add the connection and keep-alive as the noise in the test-1.yml on line 42 under header.Date. The file would look like:-

        noise:
        |   - header.Date
        |   - header.Keep-Alive
        |   - header.Connection

Now, let's run the keploy in test mode again:-

Testrun

Voila!! Our testcases has passed 🌟


Running sample app using docker

Keploy can be used on Linux & Windows through Docker, and on MacOS by the help of Colima.

Create Keploy Alias

We need create an alias for Keploy:

alias keploy='sudo docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v "$(pwd)":/files -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock --rm ghcr.io/keploy/keploy'

Let's start the MongoDB Instance

docker-compose up -d

Capture the testcases

  1. We first need to build dockerimage of our application:-
docker build -t node-app:1.0 .
  1. Now we will run the keploy in record mode:-
keploy record -c "docker run -p 8000:8000 --name nodeMongoApp --network keploy-network node-app:1.0"

Let's generate the testcases.

Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

curl --request POST \
--url http://localhost:8000/students \
   --header 'content-type: application/json' \
   --data '{
    "name":"John Doe",
    "email":"john@xyz.com",
    "phone":"0123456798"
    }'

we will get the output:

Student registration successful!

We will get the following output in our terminal

Testcase

Running the testcases

keploy test -c "docker run -p 8000:8000 --name nodeMongoApp --network keploy-network node-app:1.0" --delay 10

Our testcases will fail as the Keep-Alive connection won't be available when we are using testmode, this happen because in test mode the Keploy uses the Mocks.yml, which was generated in the record mode.

Testcase

Let's add the connection and keep-alive as the noise in the test-1.yml on line 42 under header.Date. The file would look like:-

        noise:
        |   - header.Date
        |   - header.Keep-Alive
        |   - header.Connection

Now, let's run the keploy in test mode again:-

Testrun

Voila!! Our testcases has passed 🌟