Welcome to the final part of this review of the Pluralsight course Creating JavaScript Modules with Browserify by Jeff “coding with spike” Valore.
Jeff has over 15 years of experience in software development including Java, C#, JavaScript, CoffeeScript, and TypeScript.
His belief that clean, well-organized code is key to making software maintainable has led him to focus on unit testing and solid programming practices and principles as a cornerstone of everyday coding.
He is also the author of Require JS: Dependency Injection and Module Loading
Also in this series:
Part 2 – Easing the Development Process
Part 3 – Unit Testing Browserify Modules
This episode takes a look at the final module of the course:
Unit Testing Browserify Modules
Jasmine
Jasmine is Jeff’s unit testing framework of choice here. You can learn much more about Jasmine in Testing Clientside JavaScript, however Jeff explains the basics for you here.
We begin by installing grunt-contrib-jasmine which allows us to run our jasmine specs headlessly through phantom JS. Jeff shows us the configuration updates that we must make to our grunt file.
Our first spec just tests that true is equal to true, and on the command line we can type “grunt jasmine” and see that this test passes.
To increase our productivity we can use a shorter word as an alias for grunt jasmine. Jeff shows us how to alias it with the word “test”, and from now on we type “grunt test” to run our specs.
Jeff also gives a quick introduction to Jasmine spies.
Unit Testing a CommonJS module
We create taskModule.spec.js and write our tests.
We learn that we can’t run them yet because the spec file needs to be turned into a browserify bundle first. This is because the require function that we are using in our spec file is only made available by using browserify.
Jeff shows how to update our grunt file to browserify all of our spec files and make them available to Jasmine.
We learn that it is a best practice to have modules in objects that don’t maintain state. Most importantly, we must be aware that all exported modules are singletons, so changing a value in one test could affect another test in a bad way.
Mocking Modules
These plugin names just keep getting more interesting. Here we learn about Proxyquireify – Proxy require for Browserify. We use this to inject mock modules into the module cache.
Once installed, we see that it is quite easy to update our Grunt file to use Proxyquireify.
At the top of our spec file we add:
var proxyquire = require(‘proxyquireify’)(require);
With this done Jeff shows how to define mocks in our specs.
This completes our review of Jeff Valore’s Creating JavaScript Modules with Browserify, and I would like to thank him for creating another very informative and useful course