Developer(s) | Pivotal Labs |
---|---|
Initial release | September 14, 2010 [1] |
Stable release | 4.0.1 / February 22, 2022 [2] |
Repository | |
Written in | JavaScript |
Operating system | Cross-platform |
Type | Unit test |
License | MIT License [3] |
Website | jasmine |
Jasmine is an open-source testing framework for JavaScript. [4] 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. [5]
The developers at Pivotal Labs for Jasmine previously developed a similar unit testing framework called JsUnit before active development of Jasmine. [6]
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
functionhelloWorld(){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 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. [7] 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 comes with an inbuilt test runner. Jasmine tests can run browser tests by including a simple SpecRunner.html [9] 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, [10] 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. [11]
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 has 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. |
OCaml is a general-purpose, high-level, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.
NUnit is an open-source unit testing framework for the .NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.
Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries. Groovy uses a curly-bracket syntax similar to Java's. Groovy supports closures, multiline strings, and expressions embedded in strings. Much of Groovy's power lies in its AST transformations, triggered through annotations.
The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.
Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD).
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
ColdBox is a free, open-source, conventions-based, modular web application framework intended for building enterprise applications with ColdFusion (CFML) using a Hierarchical MVC approach.
Opa is an open-source programming language for developing scalable web applications.
Dart is a programming language designed by Lars Bak and Kasper Lund and developed by Google. It can be used to develop web and mobile apps as well as server and desktop applications.
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-side.
Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains HTML, CSS and (optionally) JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components.
Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.
In certain computer programming languages, the Elvis operator, often written ?:
, is a binary operator that returns the evaluated first operand if that operand evaluates to a value likened to logically true, and otherwise returns the evaluated second operand. This is identical to a short-circuit or with "last value" semantics. The notation of the Elvis operator was inspired by the ternary conditional operator, ? :
, since the Elvis operator expression A ?: B
is approximately equivalent to the ternary conditional expression A ? A : B
.
Unit.js is an open source unit testing framework for the JavaScript programming language, running on Node.js and the browser.
Mocha is a JavaScript test framework for Node.js programs, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library.
A headless browser is a web browser without a graphical user interface.
PhantomJS is a discontinued headless browser used for automating web page interaction. PhantomJS provides a JavaScript API enabling automated navigation, screenshots, user behavior and assertions making it a common tool used to run browser-based unit tests in a headless system like a continuous integration environment. PhantomJS is based on WebKit making it a similar browsing environment to Safari and Google Chrome. It is open-source software released under the BSD License.
Cuppa is a behavior-driven development (BDD) unit testing framework for the Java programming language version 8. The framework uses features introduced in version 8 of the language, such as lambdas. It is inspired by Mocha.