Chai
BDD and TDD assertion library for Node.js and browsers with a readable expect and should syntax.
Official Documentation ↗chai v5.x(ESM)/ Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install chaipnpm add chaiyarn add chaiMethods
No methods match this filter.
Overview
Chai is an assertion library that pairs with any test runner. Its readable chains express intent clearly, and plugins extend it for promises, HTTP responses and more.
Core Methods
expect wraps a value and exposes chains such as to.equal, to.deep.equal, to.throw and to.include. Plugins register additional assertions, and configuration controls strictness.
Usage Notes
Standardize on one assertion style, prefer deep equal for structures, and add chai-as-promised for async assertions. Assert one behavior per test for clearer failures.
Common Pitfalls
Reference equality is a common trap for objects. Chai lacks built-in promise support without a plugin. Version 5 is ESM-only, which breaks CommonJS require unless you adapt the module setup.
FAQ
- expect, should or assert?
- expect and should share the chainable BDD style while assert uses function calls. Pick one per project for consistency; expect works with any value and does not extend Object.prototype like should.
- How do I test async failures?
- With chai-as-promised, use expect(promise).to.be.rejectedWith(Error). Without the plugin, await the promise and assert inside a try/catch that the error matches.
- Why does to.equal fail for objects?
- equal checks reference identity, so two distinct but structurally equal objects fail. Use deep.equal to compare values instead of references.
Report a problem with this API docOpens a pre-filled issue on GitHub.