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