Mocha
一句话解释
Mocha 是 lightweight 测试 runner 那个 lets 你选择你的 own assertion 库 (Chai) and mocking 库 (Sinon).
用大白话说
Mocha 是 minimalist 测试 runner. Unlike Jest 哪个包含一切, Mocha 仅运行测试 — 你 bring 你的 own assertion 库 (通常 Chai) and mocking 库 (通常 Sinon).
This 'bring 你的 own tools' 方法 gives 你 flexibility. 你能 mix and match 库你 prefer. 但它 也平均值更多设置: 你安装 and 配置 Mocha, Chai, Sinon, and coverage tool separately.
Mocha 是 popular in 节点.js backend 世界 and for teams 那个想要 fine-grained control over 它们的测试 stack. For 初学者, Jest's 所有-in-one 方法是 通常 simpler.
示例
javascript
// math.test.js — Mocha + Chai
const { expect } = require('chai');
const { add } = require('./math');
describe('add', () => {
it('returns the sum of two numbers', () => {
expect(add(2, 3)).to.equal(5);
});
it('handles negative numbers', () => {
expect(add(-1, -4)).to.equal(-5);
});
});
// Run: npx mocha math.test.js
// add
// ✓ returns the sum of two numbers
// ✓ handles negative numbers
// 2 passingMocha 使用 describe/it 块组织测试,依赖断言库如 Chai(expect)。用 npx mocha 运行并查看通过/失败报告。
相关术语
相关资源
相关课程
- Mocha 基础
获取开始使用 Mocha.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。