Chapter 8. Reference

Jasmine on the Web

The Basic Structure of a Suite

  describe("colors", function() {
      describe("red", function() {
          var red;
          beforeEach(function() {
              red = new Color("red");
          });
          afterEach(function() {
              red = null;
          });
          it("has the correct value", function() {
              expect(red.hex).toEqual("FF0000");
          });
          it("makes orange when mixed with yellow", function() {
              var yellow = new Color("yellow");
              var orange = new Color("orange");
              expect(red.mix(yellow)).toEqual(orange);
          });
      });
  });

Matchers Reference

  • toEqual checks for equality, not necessarily the same object.
  • toBe checks if two objects are the same.
  • toBeTruthy checks if a value is truthy (not just true).
  • toBeFalsy checks if a value is falsy (not just false).
  • toContain checks if a value is inside another.
  • toBeDefined checks if a value is defined.
  • toBeUndefined checks if a value is undefined.
  • toBeNull checks if a value is null.
  • toBeNaN checks if a value is NaN.
  • toBeCloseTo checks decimal proximity.
  • toMatch checks if a value matches a given regular expression.
  • toThrow checks if a function throws an error.
  • .not inverts the meaning of the following matcher.

List of Falsy Values

  • false
  • 0
  • ""
  • undefined (note that the variable undefined isn’t always undefined!)
  • null
  • NaN

Reserved Words in Jasmine

The following are words that you shouldn’t use in your code so that you don’t cause conflicts with Jasmine:

  • jasmine (and everything in its namespace)
  • describe
  • it
  • expect
  • beforeEach
  • afterEach
  • runs
  • waits
  • waitsFor
  • spyOn
  • xdescribe
  • xit

These are, of course, in addition to JavaScript’s reserved words, which are even more off-limits.