Catalog / Chai Assertion Library Cheatsheet
Chai Assertion Library Cheatsheet
A comprehensive cheat sheet for the Chai assertion library, covering various assertion styles and methods for effective testing in JavaScript.
Introduction to Chai
Chai Overview
Chai is a BDD / TDD assertion library for node and the browser that can be paired with any JavaScript testing framework. It provides a clean and readable syntax for writing test assertions. |
Chai supports several interfaces: |
Installation: |
Importing Chai:
|
Expect Style Assertions
The Basic Syntax: |
Example:
|
Assert Style Assertions
The Basic Syntax: |
Example:
|
Common Assertions with Expect
Equality Assertions
|
Checks for deep equality (using |
|
Checks for deep equality of objects and arrays |
|
Alias for |
|
Checks the type of a value |
|
Alias for |
Existence and Truthiness
|
Checks if a value is not |
|
Checks if a value is |
|
Checks if a value is strictly |
|
Checks if a value is strictly |
|
Checks if a value is truthy |
Number Assertions
|
Checks if |
|
Checks if |
|
Checks if |
|
Checks if |
|
Checks if |
Common Assertions with Assert
Equality Assertions
|
Tests shallow, coercive equality with the equal comparison operator ( == ) |
|
Tests strict equality ( === ) |
|
Tests for deep equality |
|
Tests shallow, coercive inequality with the not equal comparison operator ( != ) |
|
Tests strict inequality ( !== ) |
|
Tests for deep inequality |
Type Assertions
|
Tests if a value is truthy |
|
Tests if a value is falsy |
|
Tests if a value is strictly true |
|
Tests if a value is strictly false |
|
Tests if a value is not undefined |
|
Tests if a value is undefined |
|
Tests if a value is null |
|
Tests if a value is not null |
|
Tests if the type of value is as expected |
Number Assertions
|
Tests if a value is greater than another value |
|
Tests if a value is less than another value |
|
Tests if a value is greater than or equal to another value |
|
Tests if a value is less than or equal to another value |
Additional Chai Features
String Assertions
|
Checks if |
|
Alias for |
|
Checks if |
|
Asserts that |
|
Asserts that |
Array Assertions
|
Checks if |
|
Alias for |
|
Checks if |
|
Asserts that object has expected length. |
|
Asserts that value is an array. |
Object Assertions
|
Checks if |
|
Checks if |
|
Checks if |
|
Asserts that object has property. |
|
Asserts that object has a deep property. |
|
Asserts that object has property with expected value. |