Javascript – Testing


There is certainly no shortage of tools to choose from when testing your JavaScript, and the list seems to grow longer each day. One of the more popular behavior-driven tools is Jasmine.

Diagram of a test JavaScript file that contains one Describe function in a box and inside of that box is two more it function boxes that each contain Assertion boxes.

In Jasmine you use a describe function to create a test suite, but you’re really just creating a JavaScript function. Inside of that function, use the it function to specify one or more specs (or tests), which are also just functions. Inside of the spec function, put assertions to check whether the test worked.

This is what a basic test suite looks like with both a positive and a negative test:

describe("A test suite is just a function", function() {
    it("This is a positive test", function() {
    it("This is a negative test", function() {

Jasmine includes global functions (beforeEach and afterEach) that can be used to execute code before and after each of your test specs have run. This is useful for when you have set up and tear down code that requires a lot of resources, like logging in and out as a certain user. There are also beforeAll and afterAll functions, but these are run only once for each describe block they are part of. 

Example, let’s test these classes

class Parent {
  constructor(name) { = name;
  getName() {

class Child extends Parent {
  constructor(name) {
  getMessage() {
    return 'Hello ' + super.getName();

You can add the JavaScript code for the Jasmine test suite to a separate file. It might look something like this:

describe("Test Parent Child Classes", function() {
  it("Get Message Test", function(){
      let someone = new Child('person');
      expect(someone.getMessage()).toEqual("Hello person");