Matchers
Explore how to apply Jest matchers to compare expected and received values in TypeScript testing. Learn to use matchers such as toBe, toEqual, toContain, and toThrowError to write clear and effective tests, including error handling and testing object equality.
We'll cover the following...
Matchers
Jest uses what are known as matchers to match the expected values in a test to the received values.
Let’s have a quick look at some of these matchers.
The toBe matcher
Let’s take a look at the following code to understand the toBe matcher.
Here, we use the toBe matcher on line 2 to test whether the value 1 is the same as the value 2. Obviously, this test will fail.
When we run the above test, we can see that Jest is expecting the value 2, but it received the value 1. The interesting thing about this message is that the toBe matcher is using Object.is equality. This means that the ...