Matchers
Learn how matchers allow us to test different conditions.
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.
Press + to interact
hello_jest.spec.ts
tsconfig.json
{"compilerOptions": {"strict": true,"noImplicitAny": true,"strictNullChecks": true,"strictFunctionTypes": true,"strictPropertyInitialization": true,"strictBindCallApply": true,"noImplicitThis": true,"noImplicitReturns": true,"alwaysStrict": true,"esModuleInterop": true,"declaration": true,"target": "ES2017","jsx": "react","module": "ESNext","moduleResolution": "node"}}
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 following test ...