A sample project for testing node.js + mongoose app using jest, supertest and mongodb-memory-server.
Read more in this article on dev.to.
- mongodb-memory-server: 7+
- NodeJS: 12.22+
- Install dependencies:
npm install
-
Create
.env
file with mongodb connection string (optional). -
Run tests:
npm test
Project structure:
├── models
│ ├── User.js
│ └── Post.js
├── middlewares
│ └── auth.js // Check if user has a token
├── services
│ ├── user.js
│ └── post.js
├── tests
│ ├── db.js
│ ├── auth.test.js
│ └── post.test.js
├── app.js
├── server.js
├── package.json
├── README.md
└── ...
API endpoints:
REQUEST | URL | Description |
---|---|---|
POST | /api/user/signup | register a new user |
POST | /api/user/login | user log in |
GET | / | example of a protected route |
POST | /api/posts/create | create a new post |
GET | /api/posts/:id | get post by id |