Using Matchers
Using Matchers
Jest uses "matchers" to let you test values in different ways. There are too many different matchers to memorize them all, so this document will only try to introduce the most useful ones.
Common Matchers
The simplest way to test a value is with exact equality.
test('two plus two is four', () => { expect(2 + 2).toBe(4); });
In this code, expect(2 + 2)
returns an "expectation" object. You typically won't do much with these expectation objects except call matchers on them. In this code, .toBe(4)
is the matcher. When Jest runs, it tracks all the failing matchers so that it can print out nice error messages for you.
toBe
uses ===
to test exact equality. If you want to check the value of an object, use toEqual
instead:
登录查看完整内容