chai
Original:🇺🇸 English
Translated
Chai assertion library for JavaScript. Use for JS assertions.
1installs
Sourceg1joshi/agent-skills
Added on
NPX Install
npx skill4agent add g1joshi/agent-skills chaiTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Chai
Chai is an assertion library. It pairs naturally with Mocha. It supports TDD () and BDD (, ) styles.
assertexpectshouldWhen to Use
- With Mocha: The default pair.
- Expressive Tests: You want tests to read like English ("expect foo to be a string").
Quick Start
javascript
import { expect } from "chai";
const foo = "bar";
const beverages = { tea: ["chai", "matcha", "oolong"] };
expect(foo).to.be.a("string");
expect(foo).to.equal("bar");
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property("tea").with.lengthOf(3);Core Concepts
Styles
- Assert: Classic. .
assert.equal(foo, 'bar') - Expect: BDD. Chainable. .
expect(foo).to.be.equal('bar') - Should: BDD. Extends Object prototype. . (Less common now due to side effects).
foo.should.be.equal('bar')
Plugins
Chai has a rich ecosystem.
- : For API testing.
chai-http - : For asserting promises (
chai-as-promised).return expect(promise).to.eventually.equal(2)
Best Practices (2025)
Do:
- Stick to one style: Usually Expect. It's clean and doesn't modify prototypes.
- Use Descriptive Chains: reads better than
to.be.true.to.equal(true)
Don't:
- Don't mix Assert and Expect: Confuses the reader.