mocha-skill
mocha-skill enables you to create well-organized unit tests leveraging Mocha's hierarchical test structure and Chai's expressive assertion library. Build comprehensive test suites with clear describe blocks and individual test cases, then execute them to validate your code's behavior.
mocha-skill teaches you to structure tests using Mocha's describe/it syntax. The describe block organizes related tests into logical groups, while each it block represents a single test case. Within each it block, you write Chai assertions to validate expected behavior. For example, describe('Calculator', () => { it('should add two numbers', () => { expect(add(2, 3)).to.equal(5); }); }); runs a test that checks if your add function returns 5 when given 2 and 3.
AI-generated summary based on this skill's SKILL.md
Install
LambdaTest/agent-skills/mocha-skill · repository language: Python
git clone https://github.com/LambdaTest/agent-skills
cp -r agent-skills/mocha-skill ~/.claude/skills/mocha-skillFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do I write mocha tests with describe and it blocks?
mocha-skill teaches you to structure tests using Mocha's describe/it syntax. The describe block organizes related tests into logical groups, while each it block represents a single test case. Within each it block, you write Chai assertions to validate expected behavior. For example, describe('Calculator', () => { it('should add two numbers', () => { expect(add(2, 3)).to.equal(5); }); }); runs a test that checks if your add function returns 5 when given 2 and 3.
How do I mock functions with sinon in my mocha tests?
mocha-skill covers Sinon's mocking and stubbing capabilities for isolating dependencies during testing. Sinon lets you replace real function implementations with test doubles. You can create stubs that return predetermined values, spies that track function calls, or mocks that verify specific interactions occurred. For instance, const stub = sinon.stub(obj, 'method').returns(42); replaces obj.method with a stub that always returns 42, letting you test code that depends on that method without executing its actual logic.
What is the best way to test asynchronous code like promises and async/await with mocha?
mocha-skill demonstrates testing async patterns including callbacks, promises, and async/await syntax. Mocha automatically handles promises returned from test functions, and async/await tests work seamlessly—just declare your it callback as async and await promise-based operations. For callbacks, use Mocha's done parameter: it('loads data', (done) => { fetchData((err, result) => { expect(result).to.equal('data'); done(); }); }); For promises: it('loads data', () => { return fetchData().then(result => { expect(result).to.equal('data'); }); }); Mocha waits for promise resolution before marking the test complete.
How do I configure mocha test runner with hooks and watch mode?
mocha-skill teaches you to set up Mocha with lifecycle hooks like beforeEach, afterEach, before, and after to run setup and teardown logic around your tests. Use beforeEach to initialize test fixtures before each test runs, and afterEach to clean up afterward. Enable watch mode by running mocha --watch, which automatically reruns tests whenever your files change. Configure timeouts with mocha --timeout 5000 to set a 5-second limit per test, and use mocha --grep 'pattern' to filter and run only tests matching your pattern.
What are chai assertions and how do I use expect syntax?
mocha-skill covers Chai's expressive assertion library that pairs with Mocha for readable test validation. The expect syntax lets you chain readable assertions: expect(value).to.equal(5), expect(array).to.include(item), expect(obj).to.have.property('name'), expect(fn).to.throw(Error). Chai assertions support deep equality checks with expect(obj).to.deep.equal(expectedObj), type checking with expect(value).to.be.a('string'), and complex conditions. These fluent assertions make test code self-documenting and failures easy to diagnose.
What are best practices and anti-patterns when using mocha-skill for testing?
mocha-skill highlights key best practices: write one assertion per test when possible for clarity, use descriptive test names that explain what behavior is being validated, always restore stubs and spies in afterEach hooks to prevent test pollution, and avoid testing implementation details—focus on observable behavior instead. Anti-patterns to avoid include writing tests that depend on execution order, creating overly broad describe blocks that mix unrelated tests, forgetting to handle async properly which causes tests to pass falsely, and leaving stubs active between tests which causes unexpected side effects. Keep tests isolated, fast, and maintainable.
SKILL.md
rendered from the published skill — quoted content, verbatim
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
```javascript const sinon = require('sinon');
describe('UserService', () => { let
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 3 files
mocha-skill/SKILL.md
mocha-skill/reference/advanced-patterns.md
mocha-skill/reference/playbook.md