Jasmine
Overview
Jasmine is a behavior-driven development (BDD) framework for testing JavaScript that has no dependencies on any other framework and needs no DOM. It runs in browsers and Node.js with the same clean describe/it/expect syntax, bundling matchers, spies, clocks, and async helpers out of the box. Read this when writing unit specs, spying on functions, controlling time, or testing asynchronous code without extra assertion libraries.
Official sources
Install / setup
npm install --save-dev jasmine
Source: https://jasmine.github.io/setup/nodejs.html (run npx jasmine init to scaffold config, then npx jasmine).
Core concepts
describe / it — group specs and define expectations in readable BDD style.
expect + matchers — built-in toBe, toEqual, toContain, toThrow, and custom matchers.
- Spies —
spyOn and jasmine.createSpy stub and record function calls and returns.
- Setup/teardown —
beforeEach/afterEach/beforeAll/afterAll manage shared state.
- Async support — return a promise,
await, or use the done callback.
- Clock —
jasmine.clock() fakes setTimeout/Date for deterministic timing.
- Configuration —
spec/support/jasmine.json defines spec dirs, helpers, and patterns.
- Standalone runner — a browser distribution runs specs without Node tooling.
Best practices
Common pitfalls
- Async spec finishes before assertions run → return the promise or call
done().
- Forgetting to install the clock makes timer tests flaky →
jasmine.clock().install()/uninstall().
- Shared mutable state leaks across specs → re-initialize in
beforeEach.
Examples
describe('A suite', () => {
it('contains a spec with an expectation', () => {
expect(true).toBe(true);
});
});
Further reading
Related skills
- ../mocha — flexible runner needing a separate assertion library
- ../jest — Jasmine-derived framework with snapshots and built-in mocking
- ../vitest — Vite-native runner with a Jest-style API
1---2name: jasmine3description: Jasmine is a dependency-free behavior-driven JavaScript testing framework for browsers and Node.js with built-in matchers, spies, and async support; consult when writing describe/it specs, using expect matchers and spies, testing async code, or running standalone browser and Node suites.4domain: stack5category: testing6tags: [jasmine, javascript, bdd, node, browser-testing, spies, unit-testing]7official_sources:8 - https://jasmine.github.io/9 - https://github.com/jasmine/jasmine10 - https://jasmine.github.io/pages/getting_started.html11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/jasmine14---1516# Jasmine1718## Overview19Jasmine is a behavior-driven development (BDD) framework for testing JavaScript that has no dependencies on any other framework and needs no DOM. It runs in browsers and Node.js with the same clean `describe`/`it`/`expect` syntax, bundling matchers, spies, clocks, and async helpers out of the box. Read this when writing unit specs, spying on functions, controlling time, or testing asynchronous code without extra assertion libraries.2021## Official sources22- Docs: https://jasmine.github.io/23- Repo: https://github.com/jasmine/jasmine24- Install: https://jasmine.github.io/pages/getting_started.html2526## Install / setup27```bash28npm install --save-dev jasmine29```30Source: https://jasmine.github.io/setup/nodejs.html (run `npx jasmine init` to scaffold config, then `npx jasmine`).3132## Core concepts33- **`describe` / `it`** — group specs and define expectations in readable BDD style.34- **`expect` + matchers** — built-in `toBe`, `toEqual`, `toContain`, `toThrow`, and custom matchers.35- **Spies** — `spyOn` and `jasmine.createSpy` stub and record function calls and returns.36- **Setup/teardown** — `beforeEach`/`afterEach`/`beforeAll`/`afterAll` manage shared state.37- **Async support** — return a promise, `await`, or use the `done` callback.38- **Clock** — `jasmine.clock()` fakes `setTimeout`/`Date` for deterministic timing.39- **Configuration** — `spec/support/jasmine.json` defines spec dirs, helpers, and patterns.40- **Standalone runner** — a browser distribution runs specs without Node tooling.4142## Best practices43- Use `beforeEach` to reset state so specs stay independent (https://jasmine.github.io/tutorials/your_first_suite).44- Prefer specific matchers and write custom ones for clarity (https://jasmine.github.io/tutorials/custom_matchers).45- Spy on collaborators rather than calling real side effects (https://jasmine.github.io/tutorials/your_first_suite).46- Use the clock for time-dependent code instead of real delays (https://jasmine.github.io/tutorials/your_first_suite).4748## Common pitfalls49- Async spec finishes before assertions run → return the promise or call `done()`.50- Forgetting to install the clock makes timer tests flaky → `jasmine.clock().install()`/`uninstall()`.51- Shared mutable state leaks across specs → re-initialize in `beforeEach`.5253## Examples54```js55describe('A suite', () => {56 it('contains a spec with an expectation', () => {57 expect(true).toBe(true);58 });59});60```6162## Further reading63- https://jasmine.github.io/tutorials/your_first_suite — first suite walkthrough64- https://jasmine.github.io/api/edge/global — global matcher and spy API6566## Related skills67- ../mocha — flexible runner needing a separate assertion library68- ../jest — Jasmine-derived framework with snapshots and built-in mocking69- ../vitest — Vite-native runner with a Jest-style API