describe, it, Matchers & Setup/Teardown
Batteries Included
Unlike Mocha (no built-in assertions, usually paired with Chai), Jasmine bundles test structure, assertions (expect), and mocking (spies) in one package — no separate library needed.
describe('UserService', () => {
let service;
beforeEach(() => {
service = new UserService();
});
describe('login', () => {
it('returns a token for valid credentials', () => {
const result = service.login('ada', 'secret');
expect(result.token).toBeTruthy();
});
});
});describe() groups related specs under a label; it() defines one spec. beforeEach()/afterEach() run before/after every spec in the enclosing describe(); beforeAll()/afterAll() run exactly once for the whole suite.
toBe vs. toEqual
const a = { id: 1 };
const b = { id: 1 };
expect(a).toBe(a); // passes — same reference
expect(a).toBe(b); // FAILS — different instances
expect(a).toEqual(b); // passes — deep comparison
expect(true).toBe(true); // strict boolean check
expect(1).toBeTruthy(); // merely truthy — passes for 1, "x", {}
expect([1,2,3]).toContain(2);
expect(() => risky()).toThrow(); // pass a function reference, not a callFocusing & Skipping
xit('skipped for now', () => { ... }); // x prefix skips
fit('only this runs', () => { ... }); // f prefix focusesKeep your own version of these notes — editable, searchable, and organised by your stack.
Start free