Making testable JavaScript code
This is the story of a developer who was asked to write a text transformation code and test cover it.
The developer wrote JavaScript ES6 class with just 20 lines of code, added a unit test (Mocha, Chai) and the code coverage tool (NYC) reported all unit tests to be passing with 100 % code coverage.
Let’s see this JavaScript code:
This is the unit test:
And test passing with code coverage here:
You can see that the task was fairly simple. You need two functions makeImportant
and makeUnimportant
that in a few steps turn a string into a different string. The steps within the code are commented and the…