Chai Cheatsheet - Assertion Library
This reference is for JavaScript developers writing tests with Mocha, Jest integrations, or plain Node. Chai gives three interchangeable styles (expect, should, assert); this cheatsheet shows the same check written each way. It covers equality, deep comparison of objects/arrays, type and inclusion checks, thrown-error assertions, numeric bounds, and plugin-backed helpers such as eventually (chai-as-promised) and HTTP status (chai-http). After reading you should be able to write readable assertions for values, collections, thrown errors, and promises, and know when deep.equal beats equal.
Expect Style (BDD) 8
expect(value).to.equal(expected)expect(value).to.be.a("string")expect(array).to.include(item)expect(value).to.be.trueexpect(value).to.not.be.nullexpect(array).to.have.lengthOf(3)expect(value).to.existexpect(obj).to.have.property("key", "value")Should Style (BDD) 5
value.should.equal(expected)value.should.be.a("string")array.should.include(item)value.should.be.truevalue.should.existAssert Style (TDD) 7
assert.equal(actual, expected)assert.notEqual(actual, expected)assert.deepEqual(actual, expected)assert.isTrue(value)assert.isArray(value)assert.throws(fn)assert.fail(actual, expected, "message")Equality & Deep Comparison 6
expect(value).to.deep.equal(obj)expect(value).to.eql(obj)expect(value).to.not.equal(other)expect(obj).to.have.own.property("key")expect(obj).to.have.nested.property("a.b.c")expect(arr).to.deep.equal([1, 2, 3])Type & Inclusion 6
expect(value).to.be.an("array")expect(value).to.be.an.instanceof(Date)expect(str).to.match(/^foo/)expect(str).to.have.string("bar")expect(obj).to.include({ key: "value" })expect(arr).to.have.members([1, 2, 3])Exceptions & Throws 6
expect(fn).to.throw(Error)expect(fn).to.throw("error message")expect(fn).to.throw(/message/)expect(fn).to.not.throw()assert.throws(fn, TypeError)assert.doesNotThrow(fn)Numbers, Booleans & Plugins 6
expect(num).to.be.above(5)expect(num).to.be.at.least(5)expect(num).to.be.below(10)expect(num).to.be.within(1, 10)expect(promise).to.eventually.equal("resolved")expect(res).to.have.status(200)Tips
- expect and should are BDD style; assert is TDD style.
- should requires calling .should on an object and may error on null/undefined.
- Use .deep.equal for deep comparison; .equal for shallow comparison.
- chai-as-promised adds Promise assertions; chai-http adds HTTP request assertions.
- Chain methods like to, be, have only improve readability and do nothing functionally.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us