Jasmine is a leading open source testing framework for writing and testing automated test scripts for modern web frameworks.
Certainly, for Angular, Jasmine has become the de facto, go-to framework. The following is taken from the official website:
The idea behind writing Jasmine test scripts are behaviorally and functionally driven. Test scripts have two important elements—describe and the specs (it):
- The describe function is for grouping related specs together.
- The specs are defined by calling the it function.
Here's a sample test script, which is written in Jasmine:
describe("Test suite", function() { it("contains spec with an expectation", function() { expect(true).toBe(true); }); });
In the process of writing test specs, we have to use a lot of conditional checks to match data, elements, results, asserting conditions, and much more. The Jasmine framework provides a lot of matchers, which we can readily use while writing our test specs. In the preceding sample code, toBe is one such example of a matcher.
Here's a list of the most commonly and frequently used matchers in Jasmine:
- toBe
- toBeTruthy
- toBeFalsy
- toBeGreaterThanOrEqual
- toBeLessThanOrEqual
- toHaveBeenCalled
- toHaveClass
- toMatch
We will learn how to use these matchers in the next few sections. OK, we have written our test specs, so now what? How do we run them? What will run them for us? The answers can be found in the next section.