Jump to content

User:Samuelg123/sandbox

From Wikipedia, the free encyclopedia
Jasmine
Developer(s)Pivotal Labs
Initial releaseSeptember 14, 2010; 14 years ago (2010-09-14)[1]
Stable release
2.5.2 / September 16, 2016 [2]
Repositoryhttps://github.com/jasmine/jasmine
Written inJavaScript
Operating systemCross-platform
TypeUnit Test
LicenseMIT License
Websitehttp://jasmine.github.io/

Jasmine is an open source testing framework for JavaScript.[3] It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.[4]

History

[edit]

The developers at Pivotal Labs for Jasmine previously developed a similar unit testing framework called JsUnit before active development of Jasmine.[5]

Features

[edit]
  • Supports asynchronous testing.[6]
  • Makes use of 'spies' for implementing test doubles.[6]
  • Supports testing of front-end code through a front-end extension of Jasmine called Jasmine jQuery.[6]

Usage

[edit]

Jasmine aims to be easy to read. In Jasmine, each acceptance criterion corresponds to a test unit. A test unit is known as a spec, which is short for specification.[6]

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

function helloWorld() {
  return 'Hello world!';
}

and verifies that its output is indeed the text "Hello world!".

describe('Hello world', function() {
  it('says hello', function() {
    expect(helloWorld()).toEqual('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 simply return a Boolean value: true if the expectation is matched (a way to indicate that the test has passed) or false if the expectation doesn’t match[6]. 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[7]. The identity matcher toBe checks if two things are the same object. The condition matchers toBeTruthy, toBeFalsy evaluates if something is true or false and toBeDefined, toBeUndefined checks if something is defined or undefined. As the name suggests toBeNull checks if something is null and toBeNaN checks if something is NotANumber(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[6]. The code below verifies that "Some exception" is thrown.

describe('Expect to throw an exception', function() {
  it('throws some exception', function() {
    expect( function(){ throw(Some exception); }).toThrow(Some exception);
  });
});

Jasmine has a number of other features, such as custom matchers, spies, and support for asynchronous specifications.

Jasmine Test Runners

[edit]

Jasmine doesn’t come with an inbuilt test runner. Jasmine tests are run either by including a simple SpecRunner.html file[8] or by using Karma[9], a simple JavaScript test runner tool.

Comparison between Jasmine and Mocha[10]

[edit]
Jasmine Mocha
Jasmine comes with test doubles by using spies. Mocha does not come with a test double library, and generally uses an external library like Sinon.
Jasmine does not have a command line utility to run tests. Mocha has a command line utility to run tests.
Jasmine has assertions built into it. Mocha does not have an assertions library and uses Chai for assertions.
Jasmine does not provide the ‘fake servers’ feature. Mocha, along with Sinon has the ‘fake servers’ feature.

Benefits

[edit]
  • The aim of Jasmine is to be browser, framework, platform and language independent.[11]
  • Besides behavioral driven development, Jasmine also supports test driven development.[11]

See also

[edit]

References

[edit]
  1. ^ Davis W. Frank. "Jasmine 1.0 Released". Pivotal Labs. Retrieved 11 February 2014.
  2. ^ "Releases". GitHub. Retrieved 13 February 2017.
  3. ^ http://jasmine.github.io/
  4. ^ https://github.com/pivotal/jasmine/wiki/Background
  5. ^ Github JsUnit Project Page
  6. ^ a b c d e f Ragonha, Paulo (2013). Jasmine JavaScript Testing. Packt Publishing. ISBN 978-1782167211.
  7. ^ Hahn, Evan (2013). JavaScript Testing with Jasmine. O'Reilly Media. ISBN 978-1449356378.
  8. ^ "A simple project". GitHub. Retrieved 13 February 2017.
  9. ^ "Karma Jasmine". Retrieved 13 February 2017.
  10. ^ "Jasmine vs. Mocha". Marco Franssen. Retrieved 13 February 2017.
  11. ^ a b "Comparison: Jasmine vs Mocha vs QUnit | StackShare". Retrieved 13 February 2017.
[edit]