Jasmine

Jasmine is a behavior-driven js for testing JavaScript code. It does not depend on any other JavaScript frameworks. This open source tool does not require a DOM.

Features:

  • Low overhead, no external dependencies
  • Comes out of the box with everything need to test code
  • Run browser tests and Node.js tests using the same framework

Test Doubles

Test doubles are often compared to stunt doubles, as they replace one object with another for testing purposes, similar to how actors and actresses are replaced with stunt doubles for dangerous action scenes. In Jasmine, test doubles come in the form of spies. A spy is a function that replaces a particular function where you want to control its behavior in a test and record how that function is used during the execution of that test. Some of the things you can do with spies include:

See how many times a spy was called

Specify a return value to force your code to go down a certain path

Tell a spy to throw an error

See what arguments a spy was called with

Tell a spy to call the original function (the function it is spying on). By default, a spy will not call the original function.

Asynchronous Testing

Asynchronous testing in Jasmine 2.x and Mocha is the same.

Above, User is a constructor function with a static method get. Behind the scenes, get uses fetch which performs the XHR request. I want to assert that when getresolves successfully, the resolved value is an instance of User. Because I have stubbed out User.prototype.fetch to return a pre-resolved promise, no real AJAX request is made. However, this code is still asynchronous.

By simply specifying a parameter in the it callback function (I have called it donelike in the documentation but you can call it whatever you want), the test runner will pass in a function and wait for this function to execute before ending the test. The test will timeout and error if done is not called within a certain time limit. This gives you full control on when your tests complete. The above test would work in both Mocha and Jasmine 2.x.

Sinon Fake Server

One feature that Sinon has that Jasmine does not is a fake server. This allows you to setup fake responses to AJAX requests made for certain URLs.

In the above example, if a GET request is made to /users, a 200 response containing two users, Gwen and John, will be returned. This can be really handy for a few reasons. First, it allows you to test your code that makes AJAX calls regardless of which AJAX library you are using. Second, you may want to test a function that makes an AJAX call and does some preprocessing on the response before the promise resolves. Third, maybe there are several responses that can be returned based on if the request succeeds or fails such as a successful credit card charge, an invalid credit card number, an expired card, an invalid CVC, etc. You get the idea. If you have worked with Angular, Sinon’s fake server is similar to the $httpBackend service provided in angular mocks.

Running Tests

Mocha comes with a command line utility that you can use to run tests.

This assumes your tests are located in a directory called tests. The recursive flag will find all files in subdirectories, and the watch flag will watch all your source and test files and rerun the tests when they change.

Jasmine however does not have a command line utility to run tests. There are test runners out there for Jasmine, and a very popular one is Karma by the Angular team. Karma also allows support for Mocha if you’d like to run your Mocha tests that way.