Testing With Jasmine

Daily Standup

Today I learned about how to write tests with Jasmine. We’re starting with tests in the browser and will incorporate this into Node apps a bit later in the course. This is something I’ve been wanting to get more familiar with (after doing a bit with Mocha) for a while so glad to be diving in!

Jasmine Matchers

The main test matchers in Jasmine:

  • toBe / not.toBe - === comparison
  • toBeCloseTo - similarity
  • toBeDefined - item is not undefined
  • toBeFalsey / toBeTruthy
  • toBeGreaterThan / toBeLessThan
  • toContain - if something is contained in an array
  • toEqual - == comparison
  • jasmine.any() - checks the type of a function or array

Here are some examples:

describe("Jasmine Matchers", function() {
it("allows for === and deep equality", function() {
expect(1+1).toBe(2);
expect([1,2,3]).toEqual([1,2,3]);
});
it("allows for easy precision checking", function() {
expect(3.1415).toBeCloseTo(3.14,2);
});
it("allows for easy truthy / falsey checking", function() {
expect(0).toBeFalsy();
expect([]).toBeTruthy();
});
it("allows for easy type checking", function() {
expect([]).toEqual(jasmine.any(Array));
expect(function(){}).toEqual(jasmine.any(Function));
});
it("allows for checking contents of an object", function() {
expect([1,2,3]).toContain(1);
expect({name:'Elie', job:'Instructor'}).toEqual(jasmine.objectContaining({name:'Elie'}));
});
});

Pending Tests

One handy tidbit…if you know you want to include a test but don’t know what it should test yet, there are 3 ways to do this:

describe("Pending specs", function(){

xit("can start with an xit", function(){
expect(true).toBe(true);
});

it("is a pending test if there is no callback function");

it("is pending if the pending function is invoked inside the callback", function(){
expect(2).toBe(2);
pending();
});
});

Other Testing Info

Also learned a bit about the different kind of testing out there. So far I’ve only come across TDD (test-driven development) which is when you write unit tests first, then write the code to pass the tests. I hadn’t heard of BDD (behavior-driven development) before—the idea is that you are testing not only the expected result but also the expected behavior of whatever functionality you’re testing.

There is also integration testing, which comes into play as the codebase grows to make sure all of the components and unit tests work together as they should. There is also acceptance testing, which is using the completed application to make sure pre-defined specifications are being met. Finally, stress tests determine how well the application will handle itself during unfavorable situations (super-high traffic, systems going down, etc.).

Other Stuff

I completed another section in the course which was a review of JavaScript array methods forEach(), map(), filter(), reduce(), some(), and every(). Then looking ahead to the React method, I watched a helpful talk on YouTube about the kinds of things that are helpful to know about before diving into learning React. All of the things they said are in this course—awesome!

Up Next

Review of closures and the this keyword and then we start getting into ES6.