Relational Assertions
Compare two values or objects in terms of their relative magnitude
Learn moreWelcome 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.
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.
Assertions serve as powerful tools for debugging and testing code because they enable us to:
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.
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 moreEquality Assertions
Used to compare two values or objects for equality or inequality
Learn moreArray Assertions
Verify the state or length of an array, and its contents
Learn moreString Assertions
Verify the state of a string, its length, or match against patterns
Learn moreDate Assertions
Compare and verify the state of date objects
Learn moreException Assertions
Check if a function throws a specific exception
Learn moreProperty Assertions
Verify the state of an object’s property
Learn moreAsynchronous Assertions
Verify the resolution state of a promise
Learn moreBehavior Assertions
Check the behavior of a function or object
Learn moreUI Assertions
Verify the state of a user interface
Learn moreAPI Assertions
Verify responses from API calls
Learn more