Mocha Testing Skill
Core Patterns
Basic Test with Chai
const { expect } = require('chai');
describe('Calculator', () => {
let calc;
beforeEach(() => { calc = new Calculator(); });
it('should add two numbers', () => {
expect(calc.add(2, 3)).to.equal(5);
});
it('should throw on divide by zero', () => {
expect(() => calc.divide(10, 0)).to.throw('Division by zero');
});
});
Chai Assertions
expect(value).to.equal(5);
expect(arr).to.have.lengthOf(3);
expect(obj).to.have.property('name');
expect(str).to.include('hello');
expect(fn).to.throw(Error);
expect(arr).to.deep.equal([1, 2, 3]);
expect(obj).to.deep.include({ name: 'Alice' });
Sinon Mocking
const sinon = require('sinon');
describe('UserService', () => {
let sandbox;
beforeEach(() => { sandbox = sinon.createSandbox(); });
afterEach(() => { sandbox.restore(); });
it('fetches user from API', async () => {
const stub = sandbox.stub(api, 'get').resolves({ name: 'Alice' });
const user = await userService.getUser(1);
expect(user.name).to.equal('Alice');
expect(stub.calledOnce).to.be.true;
});
});
Async Testing
it('should fetch data', async () => {
const data = await fetchData();
expect(data).to.have.property('id');
});
it('callback style', (done) => {
fetchData((err, data) => {
expect(err).to.be.null;
done();
});
});
Anti-Patterns
| Bad |
Good |
Why |
Missing done() |
Use async/await |
Hanging tests |
| No sandbox |
sinon.createSandbox() |
Stubs leak |
Arrow in describe |
Regular function for this.timeout() |
Context |
Quick Reference
| Task |
Command |
| Run all |
npx mocha |
| Watch |
npx mocha --watch |
| Grep |
npx mocha --grep "login" |
| Timeout |
npx mocha --timeout 10000 |
| Recursive |
npx mocha --recursive |
Setup: npm install mocha chai sinon --save-dev
Deep Patterns → reference/playbook.md
| § |
Section |
Lines |
| 1 |
Production Configuration |
mocharc, NYC coverage, TypeScript |
| 2 |
Testing with Chai + Sinon |
Stubs, spies, assertions |
| 3 |
Advanced Sinon Patterns |
Fake timers, nock HTTP, sequential |
| 4 |
Async Patterns |
Promise, await, callback, events |
| 5 |
Hooks & Test Organization |
Lifecycle, nesting, grep tags |
| 6 |
Custom Reporters & Plugins |
Reporter class, root hooks |
| 7 |
Express/API Testing |
Supertest integration |
| 8 |
CI/CD Integration |
GitHub Actions, services |
| 9 |
Debugging Quick-Reference |
10 common problems |
| 10 |
Best Practices Checklist |
13 items |
1---
2name: mocha-skill
3description: >
4 Generates Mocha tests in JavaScript with Chai assertions and Sinon mocking.
5 Use when user mentions "Mocha", "Chai", "sinon", "describe/it (not Jest)".
6 Triggers on: "Mocha", "Chai", "sinon", "mocha test".
7languages:
8 - JavaScript
9 - TypeScript
10category: unit-testing
11license: MIT
12metadata:
13 source: https://skillmd.com/skills/lambdatest/mocha-skill
14 author: TestMu AI
15 version: "1.0"
16---
17
18# Mocha Testing Skill
19
20## Core Patterns
21
22### Basic Test with Chai
23
24```javascript
25const { expect } = require('chai');
26
27describe('Calculator', () => {
28 let calc;
29 beforeEach(() => { calc = new Calculator(); });
30
31 it('should add two numbers', () => {
32 expect(calc.add(2, 3)).to.equal(5);
33 });
34
35 it('should throw on divide by zero', () => {
36 expect(() => calc.divide(10, 0)).to.throw('Division by zero');
37 });
38});
39```
40
41### Chai Assertions
42
43```javascript
44expect(value).to.equal(5);
45expect(arr).to.have.lengthOf(3);
46expect(obj).to.have.property('name');
47expect(str).to.include('hello');
48expect(fn).to.throw(Error);
49expect(arr).to.deep.equal([1, 2, 3]);
50expect(obj).to.deep.include({ name: 'Alice' });
51```
52
53### Sinon Mocking
54
55```javascript
56const sinon = require('sinon');
57
58describe('UserService', () => {
59 let sandbox;
60 beforeEach(() => { sandbox = sinon.createSandbox(); });
61 afterEach(() => { sandbox.restore(); });
62
63 it('fetches user from API', async () => {
64 const stub = sandbox.stub(api, 'get').resolves({ name: 'Alice' });
65 const user = await userService.getUser(1);
66 expect(user.name).to.equal('Alice');
67 expect(stub.calledOnce).to.be.true;
68 });
69});
70```
71
72### Async Testing
73
74```javascript
75it('should fetch data', async () => {
76 const data = await fetchData();
77 expect(data).to.have.property('id');
78});
79
80it('callback style', (done) => {
81 fetchData((err, data) => {
82 expect(err).to.be.null;
83 done();
84 });
85});
86```
87
88### Anti-Patterns
89
90| Bad | Good | Why |
91|-----|------|-----|
92| Missing `done()` | Use async/await | Hanging tests |
93| No sandbox | `sinon.createSandbox()` | Stubs leak |
94| Arrow in `describe` | Regular function for `this.timeout()` | Context |
95
96## Quick Reference
97
98| Task | Command |
99|------|---------|
100| Run all | `npx mocha` |
101| Watch | `npx mocha --watch` |
102| Grep | `npx mocha --grep "login"` |
103| Timeout | `npx mocha --timeout 10000` |
104| Recursive | `npx mocha --recursive` |
105
106## Setup: `npm install mocha chai sinon --save-dev`
107
108## Deep Patterns → `reference/playbook.md`
109
110| § | Section | Lines |
111|---|---------|-------|
112| 1 | Production Configuration | mocharc, NYC coverage, TypeScript |
113| 2 | Testing with Chai + Sinon | Stubs, spies, assertions |
114| 3 | Advanced Sinon Patterns | Fake timers, nock HTTP, sequential |
115| 4 | Async Patterns | Promise, await, callback, events |
116| 5 | Hooks & Test Organization | Lifecycle, nesting, grep tags |
117| 6 | Custom Reporters & Plugins | Reporter class, root hooks |
118| 7 | Express/API Testing | Supertest integration |
119| 8 | CI/CD Integration | GitHub Actions, services |
120| 9 | Debugging Quick-Reference | 10 common problems |
121| 10 | Best Practices Checklist | 13 items |