Skip to content

Commit 7677df2

Browse files
s2ahilsarthaksarthak9Sonichigonehagup
authored andcommitted
Added docs for samples-express-mongoose.md (#401)
* updated docs Signed-off-by: s2ahil <[email protected]> * Update samples-express-mongoose.md Signed-off-by: s2ahil <[email protected]> --------- Signed-off-by: s2ahil <[email protected]> Co-authored-by: Sarthak Negi <[email protected]> Co-authored-by: Animesh Pathak <[email protected]> Co-authored-by: Neha Gupta <[email protected]>
1 parent 5a7f55c commit 7677df2

File tree

2 files changed

+281
-1
lines changed

2 files changed

+281
-1
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
---
2+
id: express-mongoose-application
3+
title: sample course selling api (express)
4+
sidebar_label: NodeJS - Express + Mongoose
5+
description: The following sample app showcases how to use NodeJS framework and the Keploy Platform.
6+
tags:
7+
- javascript
8+
- quickstart
9+
- samples
10+
- examples
11+
- tutorial
12+
keyword:
13+
- NodeJS Framework
14+
- MongoDB
15+
- NodeJS
16+
- API Test generator
17+
- Auto Testcase generation
18+
---
19+
20+
## Introduction
21+
22+
A simple sample CRUD application and see how seamlessly Keploy integrates with [Express](https://expressjs.com/) and [MongoDB](https://www.mongodb.com/). Buckle up, it's gonna be a fun ride! 🎒
23+
24+
import InstallationGuide from '../concepts/installation.md'
25+
26+
<InstallationGuide/>
27+
28+
## Get Started! 🎬
29+
30+
Clone the repository and move to express-mongoose folder
31+
32+
```bash
33+
git clone https://github.com/keploy/samples-typescript && cd samples-typescript/node-mongoose
34+
35+
# Install the dependencies
36+
npm install
37+
```
38+
39+
### Lights, Camera, Record! πŸŽ₯
40+
41+
### Docker installation and running the mongodb compass
42+
43+
## Here I'm using WSL , so follow below steps to configure docker with mongo db compass
44+
45+
**1. Install docker in your windows and follow this tutorial to connect with mongodb compass :**
46+
47+
```bash
48+
https://www.youtube.com/watch?v=NEPZqSvKx40&list=PLff_PESolMjuDXQdjiqYRW_GnDQjU32QX
49+
```
50+
51+
**2. after installing docker and running those commands in video use this command as well to create a network:**
52+
53+
```bash
54+
docker network create keploy-network
55+
```
56+
57+
> \*_url should look something like this depending on your connection you can adjust, also update your .env file with mongodb_url:`mongodb://127.0.0.1:27023/courses`._
58+
59+
#### Capturing Testcases
60+
61+
```bash
62+
keploy record -c "npm start"
63+
```
64+
65+
if using wsl use this :
66+
67+
```bash
68+
sudo -E env PATH=$PATH keploy record -c 'npm start'
69+
```
70+
71+
πŸ”₯ Challenge time! Generate some test cases. How? Just **make some API calls**. Postman, Hoppscotch or even curl - take your pick!
72+
73+
#### Let's generate the testcases.
74+
75+
Make API Calls using [Hoppscotch](https://hoppscotch.io), [Postman](https://postman.com) or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.
76+
77+
πŸŽ‰ Woohoo! With a simple API call, you've crafted a test case with a mock! Dive into the Keploy directory and feast your eyes on the newly minted `test-1.yml` and `mocks.yml` after doing below steps
78+
79+
_Time to perform API magic!_
80+
Follow the breadcrumbs... or Make more API Calls
81+
82+
Some api calls you can make
83+
84+
Get request - Get all courses
85+
86+
```bash
87+
curl --request GET \
88+
--url http://localhost:3000/courses
89+
```
90+
91+
Post request - Add a new course
92+
93+
```bash
94+
curl --location 'http://localhost:3000/courses' \
95+
--header 'Content-Type: application/x-www-form-urlencoded' \
96+
--data-urlencode 'title=react advance' \
97+
--data-urlencode 'description=advance' \
98+
--data-urlencode 'price=1000' \
99+
--data-urlencode 'published=true'
100+
```
101+
102+
Put request - Add a new course
103+
104+
- Make sure to replace id of course
105+
106+
```bash
107+
curl --location --request PUT 'http://localhost:3000/courses/6626a9cd3840cb305c0a6d52' \
108+
--header 'Content-Type: application/x-www-form-urlencoded' \
109+
--data-urlencode 'title=react advance'
110+
```
111+
112+
Delete request - Delete a course
113+
114+
- Make sure to replace id of course
115+
116+
```bash
117+
curl --location --request DELETE 'http://localhost:3000/courses/6626a9cd3840cb305c0a6d52'
118+
```
119+
120+
πŸŽ‰ Easy right! Just one API call and you've whipped up a test case with a mock. Check out the Keploy directory to find your shiny new `test-1.yml` and `mocks.yml` files.
121+
122+
```yaml
123+
version: api.keploy.io/v1beta1
124+
kind: Http
125+
name: test-1
126+
spec:
127+
metadata: {}
128+
req:
129+
method: GET
130+
proto_major: 1
131+
proto_minor: 1
132+
url: http://localhost:3000/courses
133+
header:
134+
Accept: "*/*"
135+
Accept-Encoding: gzip, deflate, br
136+
Cache-Control: no-cache
137+
Connection: keep-alive
138+
Host: localhost:3000
139+
Postman-Token: 61d4ef71-85a9-4dd9-b036-6beb0136c8d7
140+
User-Agent: PostmanRuntime/7.32.1
141+
body: ""
142+
timestamp: 2024-04-22T23:56:36.910408265+05:30
143+
resp:
144+
status_code: 200
145+
header:
146+
Access-Control-Allow-Origin: "*"
147+
Connection: keep-alive
148+
Content-Length: "740"
149+
Content-Type: application/json; charset=utf-8
150+
Date: Mon, 22 Apr 2024 18:26:36 GMT
151+
Etag: W/"2e4-bpK0GltUbFpXKUMEiWddtMBI/a4"
152+
Keep-Alive: timeout=5
153+
X-Powered-By: Express
154+
body: '{"courses":[{"_id":"66269dd4a16b2f11f9c1c0e9","title":"python course","description":"advance","price":1000,"published":true,"__v":0},{"_id":"66269e13a16b2f11f9c1c0ec","title":"react course","description":"advance","price":1000,"published":true,"__v":0},{"_id":"66269f2034c9140719a0f7de","title":"express","description":"advance","price":1000,"published":true,"__v":0},{"_id":"6626a8f2946288ed91737eb7","title":"express and react","description":"advance","price":1000,"published":true,"__v":0},{"_id":"6626a9cd3840cb305c0a6d52","title":"react and next js","description":"advance","price":1000,"published":true,"__v":0},{"_id":"6626aa43f9602455c7dac9ea","title":"react advance","description":"advance","price":1000,"published":true,"__v":0}]}'
155+
status_message: OK
156+
proto_major: 0
157+
proto_minor: 0
158+
timestamp: 2024-04-22T23:56:38.951925148+05:30
159+
objects: []
160+
assertions:
161+
noise:
162+
header.Date: []
163+
created: 1713810398
164+
curl: |
165+
curl --request GET \
166+
--url http://localhost:3000/courses \
167+
--header 'Connection: keep-alive' \
168+
--header 'User-Agent: PostmanRuntime/7.32.1' \
169+
--header 'Accept: */*' \
170+
--header 'Cache-Control: no-cache' \
171+
--header 'Postman-Token: 61d4ef71-85a9-4dd9-b036-6beb0136c8d7' \
172+
--header 'Host: localhost:3000' \
173+
--header 'Accept-Encoding: gzip, deflate, br' \
174+
```
175+
176+
Or simply wander over to your browser and visit `http://localhost:3000/courses`.
177+
178+
Did you spot the new test and mock scrolls in your project library? Awesome! πŸ‘
179+
180+
### Run Tests
181+
182+
Time to put things to the test πŸ§ͺ
183+
184+
```bash
185+
keploy test -c "npm start" --delay 10
186+
```
187+
188+
If using wsl use this :
189+
190+
```bash
191+
keploy -E env PATH=$PATH keploy test -c 'npm start' --delay 10
192+
```
193+
194+
Keploy test report:
195+
![image](https://github.com/s2ahil/samples-typescript/assets/101473078/48f2b866-04d1-433b-9270-34c15786893c)
196+
197+
> The `--delay` flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.
198+
199+
### To Run test using jest use this command :
200+
201+
```bash
202+
npm test
203+
```
204+
205+
jest test coverage report :
206+
207+
![Screenshot 2024-04-22 025850](https://github.com/s2ahil/samples-typescript/assets/101473078/f60570d0-b998-4b4a-912d-80d4c73604e3)
208+
209+
## Get Keploy jest sdk
210+
211+
```bash
212+
npm i @keploy/sdk nyc jest
213+
```
214+
215+
## Update package file
216+
217+
Update the `package.json` file that runs the application:
218+
219+
```json
220+
"scripts": {
221+
//other scripts
222+
"test": "jest --coverage --collectCoverageFrom='src/**/*.{js,jsx}'",
223+
"coverage": "nyc npm test && npm run coverage:merge && npm run coverage:report",
224+
"coverage:merge": "mkdir -p ./coverage && nyc merge ./coverage .nyc_output/out.json",
225+
"coverage:report": "nyc report --reporter=lcov --reporter=text"
226+
//other scripts
227+
}
228+
```
229+
230+
## Usage
231+
232+
For the code coverage for the keploy API tests using the jest integration, you need to add the following test to your Jest test file. It can be called as `keploy.test.js`. Jest test file. It can be called as `keploy.test.js`.
233+
234+
```javascript
235+
const {expect} = require("@jest/globals");
236+
const keploy = require("@keploy/sdk");
237+
const timeOut = 300000;
238+
239+
describe(
240+
"Keploy Server Tests",
241+
() => {
242+
test(
243+
"TestKeploy",
244+
(done) => {
245+
const cmd = "npm start";
246+
const options = {};
247+
keploy.Test(cmd, options, (err, res) => {
248+
if (err) {
249+
done(err);
250+
} else {
251+
expect(res).toBeTruthy(); // Assert the test result
252+
done();
253+
}
254+
});
255+
},
256+
timeOut
257+
);
258+
},
259+
timeOut
260+
);
261+
```
262+
263+
Now let's run jest tests along keploy using command
264+
265+
```bash
266+
npm test
267+
```
268+
269+
To get Combined coverage with keploy test coverage
270+
271+
```bash
272+
npm run coverage
273+
```
274+
275+
### Wrapping it up πŸŽ‰
276+
277+
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.πŸ˜ŠπŸš€
278+
279+
Happy coding! βœ¨πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»βœ¨

β€Žversioned_sidebars/version-2.0.0-sidebars.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"quickstart/samples-nextjs",
108108
"quickstart/samples-nodejs",
109109
"quickstart/samples-node-jwt",
110-
"quickstart/crud-nodejs"
110+
"quickstart/crud-nodejs",
111+
"quickstart/express-mongoose-application"
111112
]
112113
},
113114
{

0 commit comments

Comments
Β (0)