Introduction
Jasmine is a behavior-driven development framework for testing JavaScript code that works in Node.js and web browsers. It ships with its own assertion library, spy system, and test runner, requiring zero external dependencies. The project has been maintained since 2010 and remains widely used in Angular and standalone JS projects.
What Jasmine Does
- Provides a BDD-style
describe/it/expectsyntax for writing readable test specs - Includes a built-in assertion library with matchers like
toEqual,toBeTruthy,toThrow - Ships spies for function stubbing, call tracking, and return value control
- Runs tests in Node.js via the
jasmineCLI or in browsers via an HTML runner - Supports async specs with
donecallbacks, Promises, and async/await
Architecture Overview
Jasmine discovers spec files matching a configurable glob pattern, constructs a suite tree from nested describe blocks, and executes it blocks in declaration order. The Env object manages suite registration and execution. Matchers compare actual vs expected values and collect pass/fail results. A Reporter interface emits events (suiteStarted, specDone, jasmineDone) that renderers consume for output.
Self-Hosting & Configuration
- Install via
npm install --save-dev jasmineand runnpx jasmine initto createspec/support/jasmine.json - Configure
spec_dir,spec_files, andhelpersglobs in the JSON config - Set
random: trueto randomize spec execution order for isolation checking - Use
stopSpecOnExpectationFailureto halt a spec after the first failure - For browser testing, include
jasmine.jsandjasmine-html.jsin an HTML page
Key Features
- Zero-dependency framework with assertions, spies, and runner included
- Clock mocking via
jasmine.clock()for testing time-dependent code - Custom matcher API lets teams create domain-specific assertions
- Asymmetric matchers like
jasmine.objectContaining()for partial matching - Focused (
fdescribe/fit) and excluded (xdescribe/xit) specs for targeted runs
Comparison with Similar Tools
- Mocha — requires external assertion libraries (Chai, etc.); Jasmine includes everything out of the box
- Jest — built on Jasmine's syntax but adds snapshot testing and module mocking; Jest is heavier
- Vitest — ESM-native and Vite-integrated; Jasmine has broader runtime compatibility
- QUnit — jQuery ecosystem testing; Jasmine's BDD syntax is more expressive
- AVA — concurrent test execution by default; Jasmine runs specs sequentially
FAQ
Q: Is Jasmine used in Angular projects? A: Yes, Angular CLI ships with Jasmine as its default testing framework alongside Karma as the test runner.
Q: Can I use Jasmine with TypeScript?
A: Yes, install @types/jasmine for type definitions and use ts-node or jasmine-ts as a helper.
Q: How do spies work in Jasmine?
A: spyOn(obj, 'method') replaces the method with a spy that tracks calls and arguments. Use .and.returnValue() to control return values.
Q: Does Jasmine support parallel test execution? A: The standalone Jasmine runner executes specs serially. For parallelism, use a test runner like Karma with launchers.