Nerd For Tech
Published in

Nerd For Tech

Show some love for Mocha api tests! Setting up #1


About me


  1. NodeJS v12.6.0 or newer;
  2. NPM v6.14.0 or newer;
  3. Docker v19.03.13 or newer;
  4. Docker-compose v1.27.4 or newer;
  5. Visual Studio Code (or any other code editor of your preference);
  6. JavaScript and software development entry/junior level knowledge.

Let’s start setting up!

npm init command
installing dependencies
project structure

Initialise Express application with connection to Mongodb

express application definition — app.js
configuration file — config.js
actual http server — server.js
connection to mongodb with mongoose — mongo-connect.js

Is something missing?

docker-compose for dev env
docker-compose for tests env
new entry for tests Mongodb connection string in config.js file
docker container ls result

Create a model and register routes

Item model
Empty Items router — routes/items.js
Api routes definition and exporter file — routes/index.js
Registering it right after express.json middleware in app.js file

Time for restful api!

Mocked routes
jsonschema validator implementation
GET /api/items
GET /api/items/:id
POST /api/items
PUT /api/items/:id
DELETE /api/items/:id
error handler in app.js

That’s it.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store