Chai: Assertions for Readable JavaScript Tests
Chai makes your JavaScript tests read like sentences. It provides assertion styles like expect(value).to.equal(5) to verify code behavior in test frameworks like Mocha. The main footgun: the should style fails silently on null or undefined values.
Why it exists
Test runners like Mocha or Jest know how to run tests and report outcomes, but they don't provide the tools to check if a value is correct. You need a separate library to make these checks, or assertions. Chai provides a readable, human-friendly language to define what "correct" means for your code, replacing messy manual if/throw logic.
The mental model
Think of Chai as a fluent translator for your tests. It translates your expectations about your code's behavior into clear statements that either pass or fail with a helpful message. It offers different "dialects" for these statements—expect, should, and assert—so you can pick the one that feels most natural.
How it works
Chai offers three core assertion styles. The BDD (Behavior-Driven Development) styles are chainable and read like English. The expect style uses a function wrapper: expect(user.name).to.equal('Ada'). The should style extends every object's prototype: user.name.should.equal('Ada'). In contrast, the TDD (Test-Driven Development) assert style is a classic, non-chainable approach with static methods: assert.equal(user.name, 'Ada'). You choose one style for your project and pair it with a test runner.
When to use it
Use Chai in any JavaScript project—backend or frontend—where you are writing automated tests. Since it's framework-agnostic, it pairs well with runners like Mocha, Karma, or Jasmine. Its expressive BDD styles are particularly useful for making tests self-documenting, where the test code itself clearly describes the expected behavior of the system.
When not to use it
Avoid adding Chai if your testing framework already includes a built-in assertion library you are happy with. For example, Jest comes with its own powerful expect implementation that is very similar to Chai's. Also, the should style is often avoided in modern projects because it modifies a global prototype and fails silently on null and undefined variables, which can lead to tests that pass incorrectly.
One canonical example
A test for a function that adds two numbers shows Chai's clarity. Using the expect style, you would write a test case that calls your function, add(2, 3), and then asserts the result. The assertion itself reads like a sentence: expect(result).to.equal(5). This is far more descriptive and provides better failure messages than a manual check like if (result !== 5) throw new Error(). The test clearly states its intent: it expects the result of adding 2 and 3 to equal 5.
Interview question
Which of the following is a primary reason why the "should" assertion style is often avoided in modern JavaScript testing?
- a.It requires a separate plugin or configuration to be used effectively.
- b.It modifies global object prototypes and can fail silently when asserting on null or undefined values.Correct
- c.It is incompatible with most modern JavaScript testing frameworks.
- d.Its syntax is less expressive and harder to read than the "expect" style.
Why? this is the answer
The card explicitly states that the "should" style is often avoided because it modifies a global prototype and fails silently on null and undefined variables. While readability is a factor in choosing assertion styles, the silent failure and prototype modification are the critical technical drawbacks mentioned.
Just read this? Test yourself on what you have been reading.
Read the original → chaijs.com
- #testing
- #javascript
- #nodejs
- #tdd
- #bdd
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on testing — each one lists the topics its interview covers.
See open roles