HOME

TheInfoList



OR:

Jasmine is an
open-source Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
testing framework for
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
. 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 RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in product ...
.


History

The developers at Pivotal Labs for Jasmine previously developed a similar unit testing framework called JsUnit before active development of Jasmine.GitHub JsUnit Project Page
/ref>


Features

* Supports asynchronous testing. * Makes use of 'spies' for implementing
test double In computer programming and computer science, programmers employ a technique called automated unit testing to reduce the likelihood of bugs occurring in the software. Frequently, the final release software consists of a complex set of objects or ...
s. * Supports testing of front-end code through a front-end extension of Jasmine called Jasmine-jQuery.


Usage

Jasmine aims to be easy to read. A simple
hello world ''Hello'' is a salutation or greeting in the English language. It is first attested in writing from 1826. Early uses ''Hello'', with that spelling, was used in publications in the U.S. as early as the 18 October 1826 edition of the ''Norwich C ...
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 In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance experts, and customer representatives in a software project. It encourages teams ...
and serves as the first word in the test name, which should be a complete sentence. Usage follows syntax similar to that of
RSpec RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in product ...
. The code below tests this function function helloWorld() and verifies that its output is indeed the text "Hello world!". describe('Hello world', function() ); 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. 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. 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 Nan or NAN may refer to: Places China * Nan County, Yiyang, Hunan, China * Nan Commandery, historical commandery in Hubei, China Thailand * Nan Province ** Nan, Thailand, the administrative capital of Nan Province * Nan River People Given name ...
). 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. The code below verifies that "Some exception" is thrown. describe('Expect to throw an exception', function() ); Jasmine has a number of other features, such as custom matchers, spies, and support for asynchronous specifications.


Jasmine test runners

Jasmine comes with an inbuilt test runner. Jasmine tests can run browser tests by including a simple SpecRunner.html file or by using it as a command line test runner supported for various languages lik
Nodejs, Python, Ruby
or (old way) by using Karma, a simple JavaScript test runner tool.


Comparison between Jasmine and Mocha

Mocha is another popular Javascript testing framework. The comparison between Jasmine and Mocha is given in the table below.


Benefits

* The aim of Jasmine is to be browser, framework, platform and language independent. * Besides behavior-driven development, Jasmine also supports
test driven development Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against al ...
.


See also

*
List of JavaScript libraries This is a list of notable JavaScript libraries. Constraint programming * Cassowary (software) * CHR.js DOM (manipulation) oriented * Google Polymer * Dojo Toolkit * jQuery * midori * MooTools * Prototype JavaScript Framework Graphical/v ...
*
List of unit testing frameworks This article is a list of tables of code-driven unit testing frameworks for various programming languages. Some, but not all, of these are based on xUnit. Columns (classification) * Name: This column contains the name of the framework and wil ...
*
Mocha (JavaScript framework) Mocha is a JavaScript test framework for Node.js programs, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library. Assertion Libraries Mocha can be used with most JavaScript assertion librarie ...
*
QUnit QUnit is a JavaScript unit testing framework. Originally developed for testing jQuery, jQuery UI and jQuery Mobile, it is a generic framework for testing any JavaScript code. It supports client-side environments in web browsers, and server-sid ...
*
JavaScript framework A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and ...
*
JavaScript library A JavaScript library is a library of pre-written JavaScript code that allows for easier development of JavaScript-based applications, especially for AJAX and other web-centric technologies. Libraries With the expanded demands for JavaScript, an ea ...


References

{{reflist


External links


Jasmine website

Jasmine on GitHub

JSSpec

JSpec

ScrewUnit
JavaScript programming tools Unit testing frameworks Software using the MIT license Free software programmed in JavaScript