Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsApr 23, 2026·3 min de lecture

Jasmine — Behavior-Driven JavaScript Testing Framework

Jasmine is a batteries-included BDD testing framework for JavaScript that runs in Node.js and browsers with no external dependencies.

assetLangBanner.body

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/expect syntax 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 jasmine CLI or in browsers via an HTML runner
  • Supports async specs with done callbacks, 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 jasmine and run npx jasmine init to create spec/support/jasmine.json
  • Configure spec_dir, spec_files, and helpers globs in the JSON config
  • Set random: true to randomize spec execution order for isolation checking
  • Use stopSpecOnExpectationFailure to halt a spec after the first failure
  • For browser testing, include jasmine.js and jasmine-html.js in 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.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires