# 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. ## Install Save as a script file and run: # Jasmine — Behavior-Driven JavaScript Testing Framework ## Quick Use ```bash npm install --save-dev jasmine npx jasmine init # Create spec/helloSpec.js: echo 'describe("hello", function () { it("returns greeting", function () { expect("hello world").toContain("hello"); }); });' > spec/helloSpec.js npx jasmine ``` ## 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 - https://github.com/jasmine/jasmine - https://jasmine.github.io/ --- Source: https://tokrepo.com/en/workflows/f6d0852a-3f52-11f1-9bc6-00163e2b0d79 Author: Script Depot