I don’t remember any unit test frameworks for JavaScript environments when I first became familiar with this language(around 2008). A lot changed since then. JavaScript took over the back end (NodeJS), too, and many other revolutionary libraries/frameworks are now available.
Assuming you are already familiar with NodeJS and have written code for a while, today I am going to share about starting to perform unit testing In the NodeJS application. Just to introduce you to the alternatives, here are two different choices, both of which are very popular among the NodeJS developer community:
Jasmine: A standalone BDD-based testing framework that works for both client(browser) and server-side(NodeJS) JavaScript. It also includes easy-to-understand documentation with code snippets.
Mocha With Chai: Mocha is the test runner, a feature-rich JavaScript testing framework for both browser and NodeJS. For including the test case, we can have a variety of assertion library choices. Most(and I too) choose to use chai, which has support for both TDD and BDD-based assertions.
I started with ‘mocha with chai’ without any specific reason and haven’t experienced jasmine yet. So, this tutorial is going to reflect my choice. In case you are experienced with both and have something to share about the good/bad parts, I would love to hear!
Installing:
You can add the following dependencies in your package.json file and run “npm install” to get them installed:
This will install the modules and add them to the package.json file as well.
Write Our First Test Case:
Now, let’s create a sample test script helloTest.js and write the following codes:
var expect = require("chai").expect;
describe("ClassName", function(){
describe("MethodName", function() {
it("Description of the case we are testing", function () {
expect(true).equal(true);
});
});
});Code language:JavaScript(javascript)
As we will be using the Behavior driven approach, let’s import ‘expect'(or ‘assert’, if you want to do it in classical TDD style). We don’t need to import anything for mocha as mocha itself will run the scripts and thus will provide the import automatically.
Now, explain the tests in three different nested sections: class level, method level and test case level. A class can contain several methods, thus several nested ‘describe’ section, and a method can have several ‘test cases’ to verify edge cases/behaviors of the methods.
Sure, the currently given test case is a hard-coded one to make it pass(or make one of them false to fail the test deliberately).
Running The Test Case:
Now, it’s time to run our test case. It’s super easy: just run the ‘mocha’ command.
$mochaCode language:PHP(php)
Hola! It should work fine, and you should see some output with green marks, which means success! Here is an example screenshot with a few more test cases:
Extra Tips:
As you just ran your first test, you can go ahead and add a few more. However, I would like to add two simple tips that will help you be one step ahead.
Add a test script on package.json: You should have a set script set up within package.json, something like the below:
After this, you can run a more generic ‘npm test’ command to run your tests. It’s also standard, such as Travis-ci will run this command automatically while building your repo. Moreover, it helps you have a more generic test setup so that in case you change your test libraries/environment (with a change in package.json only), the same command can be used to run tests without breaking from outside.
Set up your project to scan all sub-directories, too: By default, mocha will run the test scripts which are on current directories but won’t for subdirectories. It can be easily fixed by adding a new file named ‘mocha.opts’ and adding ‘recursive’ option in it as below:
--recursive
Final Words:
I have liked this BDD approach as seems more readable. And mostly, both worked very nicely for me. Tor read more about the other APIs available and read the official chaijs documentation on bdd api.
Let me know what you think of it or have anything to suggest/share/feedback via commenting. Let’s not ship even a single method without a unit test. Happy coding 🙂
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Leave a Reply