mocha
1
总安装量
1
周安装量
#49837
全站排名
安装命令
npx skills add https://github.com/g1joshi/agent-skills --skill mocha
Agent 安装分布
mcpjam
1
claude-code
1
replit
1
junie
1
zencoder
1
Skill 文档
Mocha
Mocha is a flexible JavaScript test framework. Unlike Jest, it does not come with an assertion library, mocking, or snapshotting built-in. It is a runner that allows you to choose your own tools (usually Chai + Sinon).
When to Use
- Flexibility: You want to pick your assertion library (Chai, Should.js).
- Node.js Backends: standard in many Express/NestJS (legacy) setups.
- Asynchronous Testing: Historically strong support for async (though standard now).
Quick Start
// npm install --save-dev mocha chai
const assert = require("chai").assert;
describe("Array", function () {
describe("#indexOf()", function () {
it("should return -1 when the value is not present", function () {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});
Core Concepts
BDD Interface
describe (suite), it (test), before, after, beforeEach, afterEach.
Hooks
Mocha allows defining hooks at the root level or inside suites to set up/tear down environments.
Async support
Use async/await in the it block callback.
it("should save user", async function () {
const user = new User("tobi");
await user.save();
});
Best Practices (2025)
Do:
- Use
.onlyand.skip: Useful during development to run a single test (it.only(...)). - Use
nyc(Istanbul): For code coverage (since Mocha doesn’t have it built-in).
Don’t:
- Don’t use arrow functions (
() => {}) if you needthis.timeout()orthis.slow(). Mocha binds the test context tothis.