The developers at Pivotal Labs for Jasmine previously developed a similar unit testing framework called JsUnit before active development of Jasmine.3
Jasmine aims to be easy to read. A simple hello world test looks like the code below, where describe() describes a suite of tests and it() is an individual test specification. The name "it()" follows the idea of behavior-driven development and serves as the first word in the test name, which should be a complete sentence. Usage follows syntax similar to that of RSpec.
The code below tests this function
and verifies that its output is indeed the text "Hello world!".
Jasmine provides a rich set of built-in matchers. In the above example, toEqual checks the equality between the value returned from the helloWorld() function and the 'Hello world!' string. This is the same as assertions used in other testing frameworks. Jasmine matchers return a Boolean value: true if the expectation is matched (a way to indicate that the test has passed) or false if the expectation does not match.7 A good practice is to put a single expectation in an individual it() test specification.
Other built-in matchers include toBe, toBeTruthy, toBeFalsy, toContain, toBeDefined, toBeUndefined, toBeNull, toBeNaN, toBeGreaterThan, toBeLessThan, toBeCloseTo.8 The identity matcher toBe checks if two things are the same object. The condition matchers toBeTruthy, toBeFalsy evaluate if something is true or false and toBeDefined, toBeUndefined check if something is defined or undefined. As the name suggests toBeNull checks if something is null and toBeNaN checks if something is not a number (NaN). Precision matcher toBeCloseTo accepts two parameters and checks if a number is close to the first parameter, given a certain amount of decimal precision as indicated by the second parameter. Matcher toContain is used to verify that an element, object or sub-string is contained in an array, list or string.
The special built-in matcher toThrow is used to verify that an exception has been thrown.9 The code below verifies that "Some exception" is thrown.
Jasmine has a number of other features, such as custom matchers, spies, and support for asynchronous specifications.
Jasmine comes with an inbuilt test runner. Jasmine tests can run browser tests by including a simple SpecRunner.html10 file or by using it as a command line test runner supported for various languages like Nodejs, Python, Ruby, or (old way) by using Karma,11 a simple JavaScript test runner tool.
Mocha is another popular Javascript testing framework. The comparison between Jasmine and Mocha is given in the table below.12
"Home". jasmine.github.io. https://jasmine.github.io/ ↩
"Background · jasmine/Jasmine Wiki". GitHub. https://github.com/pivotal/jasmine/wiki/Background ↩
GitHub JsUnit Project Page https://github.com/pivotal/jsunit ↩
Ragonha, Paulo (2013). Jasmine JavaScript Testing. Packt Publishing. ISBN 978-1782167211. 978-1782167211 ↩
Hahn, Evan (2013). JavaScript Testing with Jasmine. O'Reilly Media. ISBN 978-1449356378. 978-1449356378 ↩
"A simple project". GitHub. https://github.com/jasmine/jasmine/wiki/A-simple-project ↩
"Karma Jasmine". 16 June 2022. https://www.npmjs.com/package/karma-jasmine ↩
"Jasmine vs. Mocha". Marco Franssen. Retrieved 13 February 2017. https://marcofranssen.nl/jasmine-vs-mocha/ ↩
"Comparison: Jasmine vs Mocha vs QUnit | StackShare". Retrieved 13 February 2017. https://stackshare.io/stackups/jasmine-vs-mocha-vs-qunit ↩