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.

Languages·44 commands·Last updated 2026-07-21
chaiTestingAssertionsjavascript

Expect Style (BDD) 8

expect(value).to.equal(expected)
Equality assertion (strict ===)
expect(value).to.be.a("string")
Type assertion
expect(array).to.include(item)
Inclusion assertion
expect(value).to.be.true
Boolean assertion
expect(value).to.not.be.null
Non-null assertion
expect(array).to.have.lengthOf(3)
Length assertion
expect(value).to.exist
Existence assertion (non-null and non-undefined)
expect(obj).to.have.property("key", "value")
Property and its value assertion

Should Style (BDD) 5

value.should.equal(expected)
Equality assertion
value.should.be.a("string")
Type assertion
array.should.include(item)
Inclusion assertion
value.should.be.true
Boolean assertion
value.should.exist
Existence assertion

Assert Style (TDD) 7

assert.equal(actual, expected)
Equality assertion
assert.notEqual(actual, expected)
Inequality assertion
assert.deepEqual(actual, expected)
Deep equality
assert.isTrue(value)
Truthy assertion
assert.isArray(value)
Array assertion
assert.throws(fn)
Throws exception assertion
assert.fail(actual, expected, "message")
Explicit failure assertion

Equality & Deep Comparison 6

expect(value).to.deep.equal(obj)
Deep-compare objects/arrays
expect(value).to.eql(obj)
Alias of deep.equal
expect(value).to.not.equal(other)
Inequality assertion
expect(obj).to.have.own.property("key")
Own property assertion (no prototype chain)
expect(obj).to.have.nested.property("a.b.c")
Nested property assertion
expect(arr).to.deep.equal([1, 2, 3])
Array deep comparison

Type & Inclusion 6

expect(value).to.be.an("array")
Array type assertion
expect(value).to.be.an.instanceof(Date)
Instance type assertion
expect(str).to.match(/^foo/)
Regex match assertion
expect(str).to.have.string("bar")
String contains substring
expect(obj).to.include({ key: "value" })
Object partial inclusion
expect(arr).to.have.members([1, 2, 3])
Array members exactly equal

Exceptions & Throws 6

expect(fn).to.throw(Error)
Throw a specific type of exception
expect(fn).to.throw("error message")
Throw with a specific message
expect(fn).to.throw(/message/)
Regex-match the exception message
expect(fn).to.not.throw()
Do not throw
assert.throws(fn, TypeError)
TDD throw assertion
assert.doesNotThrow(fn)
TDD no-throw assertion

Numbers, Booleans & Plugins 6

expect(num).to.be.above(5)
Greater-than assertion
expect(num).to.be.at.least(5)
At-least (>=) assertion
expect(num).to.be.below(10)
Less-than assertion
expect(num).to.be.within(1, 10)
Within-range (inclusive) assertion
expect(promise).to.eventually.equal("resolved")
Promise assertion (needs chai-as-promised)
expect(res).to.have.status(200)
HTTP status assertion (needs chai-http)

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