Skip to content

Assertions

Welcome to the Assertions section of our JS Assertion CheatSheet! As you embark on your journey of mastering JavaScript assertion tools, it’s crucial to lay a strong foundation by understanding what assertions are and why they’re important.

What Are Assertions?

An assertion in programming is a declarative statement that asserts that a certain condition is true. If the condition returns true, the program continues to execute as expected. If the condition returns false, the program typically throws an assertion error and often terminates.

In the context of testing, assertions serve as checkpoints that validate if the outcome of a tested operation matches our expectations. They form the basis of most types of automated testing, including unit testing, integration testing, and end-to-end testing.

In JavaScript, we use various assertion libraries (like Chai, Assert, etc.) that provide different types of assertions, such as equal, notEqual, deepEqual and many more.

Why Are Assertions Important?

Assertions serve as powerful tools for debugging and testing code because they enable us to:

  1. Check Invariants: Assertions help confirm that the program’s state aligns with our expectations at particular points during execution.
  2. Simplify Debugging: If a bug causes an assertion failure, you’ve successfully localized the bug to a specific point in the program, which makes the debugging process easier.
  3. Improve Code Quality: Planned assertion usage can lead to cleaner, less error-prone code. They serve as validation checks that prompt necessary actions to be taken to ensure smooth code execution.
  4. Document Assumptions: Assertions can be used to document assumptions made at certain points in the code about how variables should behave. This can be highly useful for understanding the logic behind the code.
  5. Automated Testing: Assertions form the backbone of test cases. They define the criteria of success for each test, allowing us to automate the validation process.

Navigating the world of JavaScript assertions may seem daunting at first, but with practice, you’ll find assertions to be a game-changer for your testing strategy. This guide aims to support you in this journey, serving as a handy resource to explore different assertion tools and practices.

Assertion Categories

When it comes to JavaScript assertions, they can be categorized based on their purpose, scope or functionality. Here’s an insight into these various categories:

Relational Assertions

Compare two values or objects in terms of their relative magnitude

Learn more

Equality Assertions

Used to compare two values or objects for equality or inequality

Learn more

Array Assertions

Verify the state or length of an array, and its contents

Learn more

String Assertions

Verify the state of a string, its length, or match against patterns

Learn more

Date Assertions

Compare and verify the state of date objects

Learn more

Exception Assertions

Check if a function throws a specific exception

Learn more

Property Assertions

Verify the state of an object’s property

Learn more

Asynchronous Assertions

Verify the resolution state of a promise

Learn more

Behavior Assertions

Check the behavior of a function or object

Learn more

UI Assertions

Verify the state of a user interface

Learn more

API Assertions

Verify responses from API calls

Learn more