It was late into the development that I realized that Automated Testing is another major part that I was missing out on.
Sure we tested all the api routes with Postman when we made them, but this is not a sustainable approach.

I understood this when I slightly tweaked one of the Controllers, which in turn broke the working of some other controllers that my teammates had worked on and depended on that one.

Cat Meme

Even though this was a rare case because we worked on completely seperate parts of the website, I thought that I should get some practice building tests since I had never used them before.

That’s where Mocha and Chai come in.
Mocha is a testing Framework which provides functions executed in a serial fashion. Chai is an assertions library which provides functions that compare the actual values and expected values. Both of these are used together to develop test suites.
So I setup my test suite in the following way :

Backend 
    ├── test
          ├── test.js
          ├── event.js
          ... Other Test Files ...
    ├── controllers
    ...

A sample test file looks like this :

let chai = require('chai');
let chaihttp = require('chai-http');
let should = chai.should();

describe('Login', ()=>{
    before(()=>{
        CreateUser("testuser");
    });
    it("Should Not Login",(done)=>{
        chai.request(app)
        .post('/api/signin')
        .set('Accept', 'application/json')
        .set('Content-Type', 'application/json')
        .send({email:"[email protected]", password:"wrongpassword"})
            .end((ignore,res)=>{
                res.should.not.have.status(200);
                done();
            });
    });
});

In my package.json file, Mocha is called with the test.js file as argument mocha test.js, the test.js calls require() on all the other test files using which I could specify the order of test execution.
I did the tests this way because I needed to call some common functions to kill the server and hide console (using Sinon) which would be present in all test files and also call some functions before and after all tests like add some users to the database and clean up the database after all the tests are executed.

PS. Are you curious how to type those and symbols? Hold Alt and type 195 () or 196 () on the numpad. You can type all ASCII characters that way…

You Though I was Done? More Here