Jasmine Testing Skill
Core Patterns
Basic Test
describe('Calculator', () => {
let calc;
beforeEach(() => { calc = new Calculator(); });
it('should add two numbers', () => {
expect(calc.add(2, 3)).toBe(5);
});
it('should throw on divide by zero', () => {
expect(() => calc.divide(10, 0)).toThrowError('Division by zero');
});
});
Matchers
expect(value).toBe(exact); // === strict
expect(value).toEqual(object); // Deep equality
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeNull();
expect(value).toBeUndefined();
expect(value).toBeDefined();
expect(value).toBeNaN();
expect(value).toBeGreaterThan(3);
expect(value).toBeCloseTo(0.3, 5);
expect(str).toContain('sub');
expect(str).toMatch(/pattern/);
expect(arr).toContain(item);
expect(fn).toThrow();
expect(fn).toThrowError('message');
// Negation
expect(value).not.toBe(other);
Spies
describe('UserService', () => {
let service, api;
beforeEach(() => {
api = jasmine.createSpyObj('api', ['get', 'post']);
service = new UserService(api);
});
it('fetches user from API', async () => {
api.get.and.returnValue(Promise.resolve({ name: 'Alice' }));
const user = await service.getUser(1);
expect(user.name).toBe('Alice');
expect(api.get).toHaveBeenCalledWith('/users/1');
expect(api.get).toHaveBeenCalledTimes(1);
});
});
// Spy on existing method
spyOn(obj, 'method').and.returnValue(42);
spyOn(obj, 'method').and.callThrough(); // Call original
spyOn(obj, 'method').and.throwError('err');
Async Testing
it('fetches data', async () => {
const data = await fetchData();
expect(data).toBeDefined();
});
// With done callback
it('fetches data', (done) => {
fetchData().then(data => {
expect(data).toBeDefined();
done();
});
});
// Clock control
beforeEach(() => { jasmine.clock().install(); });
afterEach(() => { jasmine.clock().uninstall(); });
it('handles timeout', () => {
const callback = jasmine.createSpy();
setTimeout(callback, 1000);
jasmine.clock().tick(1001);
expect(callback).toHaveBeenCalled();
});
Setup: npm install jasmine --save-dev && npx jasmine init
Run: npx jasmine or npx jasmine spec/calculatorSpec.js
Deep Patterns
See reference/playbook.md for production-grade patterns:
| Section |
What You Get |
| §1 Project Setup |
jasmine.json, TypeScript, spec reporter config |
| §2 Spies — Complete API |
spyOn, createSpyObj, callFake, returnValues, call tracking |
| §3 Async Testing |
async/await, expectAsync, promise matchers |
| §4 Custom Matchers |
Domain-specific matchers, asymmetric matchers |
| §5 Test Organization |
Nested describe, shared state, focused/excluded |
| §6 Fetch & Module Mocking |
globalThis.fetch spy, HTTP error handling |
| §7 Browser Testing |
DOM creation, keyboard events, focus trapping with Karma |
| §8 CI/CD Integration |
GitHub Actions with coverage, browser testing |
| §9 Debugging Table |
12 common problems with causes and fixes |
| §10 Best Practices |
14-item checklist for production Jasmine testing |
1---
2name: jasmine-skill
3description: >
4 Generates Jasmine tests in JavaScript. BDD-style framework with spies and
5 async support. Use when user mentions "Jasmine", "jasmine.createSpy",
6 "toHaveBeenCalled". Triggers on: "Jasmine", "jasmine test", "createSpy",
7 "Jasmine spec".
8languages:
9 - JavaScript
10 - TypeScript
11category: unit-testing
12license: MIT
13metadata:
14 source: https://skillmd.com/skills/lambdatest/jasmine-skill
15 author: TestMu AI
16 version: "1.0"
17---
18
19# Jasmine Testing Skill
20
21## Core Patterns
22
23### Basic Test
24
25```javascript
26describe('Calculator', () => {
27 let calc;
28
29 beforeEach(() => { calc = new Calculator(); });
30
31 it('should add two numbers', () => {
32 expect(calc.add(2, 3)).toBe(5);
33 });
34
35 it('should throw on divide by zero', () => {
36 expect(() => calc.divide(10, 0)).toThrowError('Division by zero');
37 });
38});
39```
40
41### Matchers
42
43```javascript
44expect(value).toBe(exact); // === strict
45expect(value).toEqual(object); // Deep equality
46expect(value).toBeTruthy();
47expect(value).toBeFalsy();
48expect(value).toBeNull();
49expect(value).toBeUndefined();
50expect(value).toBeDefined();
51expect(value).toBeNaN();
52expect(value).toBeGreaterThan(3);
53expect(value).toBeCloseTo(0.3, 5);
54expect(str).toContain('sub');
55expect(str).toMatch(/pattern/);
56expect(arr).toContain(item);
57expect(fn).toThrow();
58expect(fn).toThrowError('message');
59
60// Negation
61expect(value).not.toBe(other);
62```
63
64### Spies
65
66```javascript
67describe('UserService', () => {
68 let service, api;
69
70 beforeEach(() => {
71 api = jasmine.createSpyObj('api', ['get', 'post']);
72 service = new UserService(api);
73 });
74
75 it('fetches user from API', async () => {
76 api.get.and.returnValue(Promise.resolve({ name: 'Alice' }));
77 const user = await service.getUser(1);
78 expect(user.name).toBe('Alice');
79 expect(api.get).toHaveBeenCalledWith('/users/1');
80 expect(api.get).toHaveBeenCalledTimes(1);
81 });
82});
83
84// Spy on existing method
85spyOn(obj, 'method').and.returnValue(42);
86spyOn(obj, 'method').and.callThrough(); // Call original
87spyOn(obj, 'method').and.throwError('err');
88```
89
90### Async Testing
91
92```javascript
93it('fetches data', async () => {
94 const data = await fetchData();
95 expect(data).toBeDefined();
96});
97
98// With done callback
99it('fetches data', (done) => {
100 fetchData().then(data => {
101 expect(data).toBeDefined();
102 done();
103 });
104});
105
106// Clock control
107beforeEach(() => { jasmine.clock().install(); });
108afterEach(() => { jasmine.clock().uninstall(); });
109
110it('handles timeout', () => {
111 const callback = jasmine.createSpy();
112 setTimeout(callback, 1000);
113 jasmine.clock().tick(1001);
114 expect(callback).toHaveBeenCalled();
115});
116```
117
118## Setup: `npm install jasmine --save-dev && npx jasmine init`
119## Run: `npx jasmine` or `npx jasmine spec/calculatorSpec.js`
120
121## Deep Patterns
122
123See `reference/playbook.md` for production-grade patterns:
124
125| Section | What You Get |
126|---------|-------------|
127| §1 Project Setup | jasmine.json, TypeScript, spec reporter config |
128| §2 Spies — Complete API | spyOn, createSpyObj, callFake, returnValues, call tracking |
129| §3 Async Testing | async/await, expectAsync, promise matchers |
130| §4 Custom Matchers | Domain-specific matchers, asymmetric matchers |
131| §5 Test Organization | Nested describe, shared state, focused/excluded |
132| §6 Fetch & Module Mocking | globalThis.fetch spy, HTTP error handling |
133| §7 Browser Testing | DOM creation, keyboard events, focus trapping with Karma |
134| §8 CI/CD Integration | GitHub Actions with coverage, browser testing |
135| §9 Debugging Table | 12 common problems with causes and fixes |
136| §10 Best Practices | 14-item checklist for production Jasmine testing |