Skip to content

How and why you should add unit testing to your node app

Brian Bawuah edited this page Jun 17, 2020 · 3 revisions

Why should we even test in the first place?

We use unit testing to test our functions

Mocha

Mocha is a javascript testing framework that runs on Node.js. With Mocha you can test to check that your functions are doing what they are supposed to do

It is best practise to keep the test environment in your devDependencies.

npm install --save-dev mocha chai

After that, I created a folder in the root of our project called 'test'

The text folder is a default folder for Mocha

Next I added a test script to our package.json

{
  "name": "pixby",
  "version": "1.0.0",
  "description": "Only baby pictures allowed.",
  "main": "",
  "scripts": {
    "start": "node index.js",
    "test": "mocha",
    "reload": "nodemon"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/joannlap/Pixby.git"
  },
  "author": "joannlap",
  "license": "MIT",
  "homepage": "https://github.com/joannlap/Pixby",
  "dependencies": {
    "axios": "^0.19.2",
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "handlebars": "^4.7.6",
    "hbs": "^4.1.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.5.7",
    "mongoose": "^5.9.18",
    "multer": "^1.4.2"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.1.0",
    "eslint-plugin-import": "^2.20.2",
    "mocha": "^8.0.1",
    "nodemon": "^2.0.4"
  }
}

Recourses