Jasmine
Jasmine is a behavior-driven development (BDD) framework for testing JavaScript code. It provides a clean, readable syntax using functions like `describe`, `it`, and `expect` to structure tests and make assertions. It is framework-agnostic and can be used to test any JavaScript application.
First used·2010
Definitions·1
Synonyms·1
Category·Software Testing
Also known as
Definitions
What it means.
- 01
Jasmine as a Behavior-Driven Development (BDD) Framework
Jasmine is a popular open-source testing framework for JavaScript. It is designed for Behavior-Driven Development (BDD), which focuses on describing the behavior of a piece of software in a clear, human-readable format.
Jasmine's syntax is clean and descriptive, making tests easy to write and understand. The core structure revolves around suites and specs. A test suite, initiated with the
describe()function, groups together related tests. Each individual test case, or spec, is defined with theit()function. This structure reads like a sentence, for example: "describe the calculator, it should add two numbers."Assertions in Jasmine are called expectations and are written using the
expect()function combined with a "matcher" function. For instance,expect(result).toBe(5);checks if theresultvariable is strictly equal to 5. Jasmine comes with a rich library of built-in matchers liketoEqual(),toBeTruthy(),toContain(), and more.Jasmine also includes built-in support for test doubles, known as spies. A spy, created with
spyOn(), can track calls to a function, its arguments, and even replace its implementation. This is crucial for isolating units of code during testing, allowing you to test a function's interaction with its dependencies without actually executing those dependencies.
Origin
Where it comes from.
Etymology
The name 'Jasmine' was chosen by its creators at Pivotal Labs. It does not have a specific technical origin but was selected to be a pleasant and memorable name, following a trend in the software community of using non-technical, often nature-inspired names for projects.
Historical context
Jasmine was created by Pivotal Labs (now part of VMware) and first released in 2010. It emerged during a period when JavaScript applications, particularly Single-Page Applications (SPAs), were growing in complexity, creating a pressing need for robust testing tools.
Inspired by unit testing frameworks like JUnit for Java, Jasmine brought a similar structure but with a more expressive, BDD-style syntax to the JavaScript ecosystem. It was designed from the ground up to be independent of any other JavaScript framework, browser, or DOM, making it a versatile tool for any JavaScript project.
Its popularity surged when it was adopted as the default testing framework for AngularJS. The clean describe, it, and expect syntax became so influential that it was later adopted and popularized by other major testing frameworks, most notably Jest. Jasmine established a standard for how JavaScript testing should look and feel.
Usage
In context.
Our team uses Jasmine to write unit tests for our Angular application, ensuring each component functions as expected.
The developer wrote a new test suite in Jasmine using
describeanditblocks to verify the new API endpoint's logic.To mock a dependency in our test, we used a Jasmine spy with
spyOnto track calls to the function without executing its original code.
FAQ
Common questions.
The three main keywords are describe, it, and expect.
describeis used to group related tests into a test suite.itdefines an individual test specification or test case.expectis used to create an assertion, which is then chained with a matcher function (like.toBe()or.toEqual()) to verify a specific outcome.
Taxonomy
Filed under.
Categories
Tags