Over a million developers have joined DZone.

JavaScript API Testing

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Testing your JavaScript Library/API

I wrote a small JavaScript API, HtmlAssert.js, because I needed to be able to test HTML content in JavaScript for a project I’m working on.

I did it TDD style, with Grunt, Jasmine and PhantomJS.

I used Jasmine to write my unit tests because this API validates HTML content, so I am using the DOM of the HTML document, and in particular, the window.document element to generate an HTML document that I can easily parse in JavaScript.

Therefore, in order to have the window object, tests need to be executed in a browser (I tried to use jsdom but the C++ compilation part is not straightforward at all, and I don’t have much time to waste).

So with Jasmine, everything was cool, I had my library and my unit tests.

Then came the time to include that API in the project I am working on, and I realized that the best was probably to create a Node.js module.

It is when the chaos started, because my Jasmine unit tests couldn’t be used for a Node.js module with my project stack.

Client-side Testing vs Server-side Testing

When you test client-side, you are basically executing your test in a browser and so you have access to the window object.

When you test server-side, you are executing your test in a JavaScript only stack and you can’t access to the window object.

Jasmine is the testing library that I use to execute my tests.

Grunt is the utility I use to execute my tasks, in particular executing jasmine to run my tests.
When executed from Grunt, grunt-contrib-jasmine is executing tests client-side using PhantomJs as a (headless) browser.

Which means that you don’t have access to Node.JS modules support and the require(“module”); won’t work.

And that is a problem, since you want to use this, in order to test your node module, in the same condition than when you will use your module in a real project.

The other way, running jasmine test server-side is not a good solution either, because you won’t have access to the browser’s window object, so if you are testing browser-related code, it will not work.

Finally I manage to find the solution, using CasperJS, that allows me to run test with node modules and have access to the browser (PhantomJS too) even on the server side.


You can look at my project HtmlAssert.js. The Gruntfile.js has the definition of the CasperJS task, and inside the /test directory you will see how I wrote my tests using CasperJS’s BDD capabilities.


Please note that I’m a Java developer learning modern JavaScript, so if you are experienced with JavaScript, and any part of my explanations seems weird to you, do not hesitate to give your comments!!! Thanks!

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}