AN INTERESING TOOL I ENCOUNTERED DURING ANDELA BOOTCAMP PREPARATION.
An interesting tool I encountered during preparation for bootcamp is the mocha framework. Mocha is a tool which allows developers to write preconceived tests for various scenarios and functionalities of a project. A question that bugs the mind arises, how could it be possible to test something that as not been implemented yet? There as to be a prior implementation before tests can be carried out.
This is where the whole concept of TDD(i.e. TEST DRIVEN DEVELOPMENT) comes in . Consider a scenario where you intend to develop a simple login application. The old method would have involved going straight into detailing specifications and into writing functions for validation, authentication e.t.c . In test driven development, this is not the case.
TDD encourages detailing specific criterias which our project is meant to meet and writing tests initial tests which will fail at first. Thus this creates an avenue to know the minimum criterias which a particular feature must pass before the development even begins.
Mocha is a framework which enable developers to write easily understandable tests in TDD using keywords which are simplified and easy to understand. Mocha is used to run the tests and Chai.js is a library that we use to write the test assertions. Chai.js implements three different assertion styles:
Which one you choose is just a matter of preference.
To install mocha in nodejs application, assuming we have node and npm installed,we need to install Mocha and Chai.js. We also need to install Babel, which is a transpiler that translates ES6 code to ES5 so it can be executed in Node.js. Integration of mocha for testing is as straight forward as can be. In the node CLI just run
$ npm install — save-dev mocha chai babel
A detailed understanding of mocha terminology can be gotten from the mocha documentation. Mocha is a really great tool as it not only makes writing tests more efficient, its simplification of testing terminologies is unparalleled.