An up-to-date 2021 example of how you can use Mocha and Chai to perform API testing for a Node-ExpressJS-Mongoose app. You will be able to test your code locally and also automatically using Github Action.
Read the Test Guide for instructions on how to perform local and automated testing.
In this project:
test/productRoutes.test.tscontains unit and integration tests forsrc/routes/productRoutes.ts.src/routes/productRoutes.tscontains routes which interact withsrc/services/ProductService.ts.src/services/ProductService.tscontains service methods which interact withsrc/models/Product.ts.
View the Test Plan for more information.
Stable release: v0.1.3
- Implementation of
GET,POST,PUTandDELETEroutes - 16 unit test cases testing the API routes individually
- 6 integration test cases testing route pairs (e.g.
GETandPUT)
Current release: v0.1.3
This project will not be frequently worked on beyond v0.1.0.
If you have any suggestions, bugs to report or would like to contribute to this project, feel free to create an Issue or Pull Request.
What is Mongoose
MongooseJS provides a straight-forward solution to modeling your NodeJS application data easily in MongoDB. It handles all MongoDB validation, casting and business logic on your behalf.
What is Mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser. It is the test environment of choice for this project.
What is Chai
Chai is an assertion library for Node. Chai assertions and chai-http are used in Mocha to perform HTTP testing of the NodeJS API endpoints.