Skip to content

Latest commit

 

History

History
185 lines (127 loc) · 5.32 KB

README.md

File metadata and controls

185 lines (127 loc) · 5.32 KB

Express-Mongo

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

Install Keploy

Install keploy via one-click:-

curl --silent -O -L https://keploy.io/install.sh && source install.sh

Setup application

Clone the repository and move to express-mongo folder

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

# Install the dependencies
npm install

Let's start the MongoDB Instance

docker-compose up -d mongo

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/Students to mongodb://127.0.0.1:27017/keploy.

Capture the testcases

sudo -E env PATH=$PATH keploy record -c 'node 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):$(pwd) -w $(pwd) -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'

Capture the testcases

We will run the keploy in record mode with docker-compose to start our application:-

keploy record -c "docker compose up" --containerName "nodeMongoApp"

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 compose up --node-app" --containerName "nodeMongoApp" --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 🌟

Create Unit Testcase with Keploy

Prequiste

AI model API_KEY to use:

  • OpenAI's GPT-4o.
  • Alternative LLMs via litellm.

Setup

Get API key from OpenAI or API Key from other LLM

export API_KEY=<LLM_MODEL_API_KEY>

Generate Unit tests

Let's check the current code coverage of out application : -

npm test

We got around 31.5% of code coverage.

Npm Test

Now, let's run keploy to create testcases.

keploy gen --sourceFilePath="/home/sonichigi.linux/samples-typescript/express-mongoose/src/routes/routes.js" --testFilePath="/home/sonichigi.linux/samples-typescript/express-mongoose/test/routes.test.js" --testCommand="npm test" --coverageReportPath="/home/sonichigi.linux/samples-typescript/express-mongoose/coverage/cobertura-coverage.xml"

With the above command, Keploy will generate new testcases in the our routes.test.js and will increase code coverage upto 58%.

Keploy Mux UTG