as everyone knows , In the previous code , We used it when writing a test case test,expect,toBe Three methods , We also introduced before Jest Basic use of ,Jest
Simple configuration of , Today, let's talk about Jest Matcher in (matchers).

This grammar can probably be translated into : Expect the result of one value to match another .

such as :
expect(1 + 1).toBe(2);
This example can be translated as : expect 1 + 1 The result is 2, there toBe It's a matcher , Used to judge whether the received value is equal to the expected value .

in fact , stay Jest in , except toBe outside , There are also many matchers .

<>toBe

Test whether the values of two objects are equal , be similar to js Medium === operator .
// toBe test(" Test the commutative law of addition ", () => { for (let x = 0; x <= 10; x++) { for (let y = 0;
y<= 10; y++) { expect(x + y).toBe(y + x); } } });
<>toEqual

Test whether the original values of two objects are equal , Check content only , Do not check references .
// toEqual const can1 = { value: "hello" }; const can2 = { value: "hello" };
test(" test can1 and can2 Are the contents of equal ", () => { expect(can1).toEqual(can2); });
<>toBeNull

Whether the value of the test object is null, The effect is equivalent to .toBe(null).
// toBeNull const value = null; test(" Whether the test value is null", () => { expect(value).
toBeNull(); });
<>toBeUndefined

Whether the value of the test object is undefined, The effect is equivalent to .toBe(undefined).
// toBeUndefined const value = undefined; test(" Whether the test value is undefined", () => {
expect(value).toBeUndefined(); });
<>toBeDefined

Is the test value defined , except undefined Will pass the test except for .
// toBeDefined const value = 1; test(" Is the test value defined ", () => { expect(value).
toBeDefined(); });
<>toBeTruthy

Check whether the value is true after being converted to Boolean value .

<>toBeFalsy

Check whether the value is false after being converted to Boolean value .
// toBeTruthy,toBeFalsy test(" Test whether it is true ", () => { expect(0).toBeTruthy(); // Fail
expect("").toBeTruthy(); // Fail expect(null).toBeTruthy(); // Fail expect(false).
toBeTruthy(); // Fail expect(undefined).toBeTruthy(); // Fail }); test(" Test whether it is false ",
() => { expect(0).toBeFalsy(); // adopt expect("").toBeFalsy(); // adopt expect(null).
toBeFalsy(); // adopt expect(false).toBeFalsy(); // adopt expect(undefined).toBeFalsy(
); // adopt });
<>not

Reverse matcher , amount to js Medium ! operator .
// not test(" Whether the test value is not aaa", () => { expect("hello").not.toBe("aaa"); }); test(
" Whether the test value is not null", () => { expect([]).not.toBeNull(); }); test(" Whether the test value is not undefined",
() => { expect({}).not.toBeUndefined(); });
<>toBeGreaterThan

Check whether the received value is greater than the expected value .
// toBeGreaterThan test(" test 10 Greater than 9", () => { expect(10).toBeGreaterThan(9);
});
<>toBeLessThan

Check whether the received value is less than the expected value .
// toBeLessThan test(" test 10 Is it less than 20", () => { expect(10).toBeLessThan(20); });
toBeGreaterThanOrEqual

Check whether the received value is greater than or equal to the expected value .
// toBeGreaterThanOrEqual test(" test 10 Is it greater than or equal to 10", () => { expect(10).
toBeGreaterThanOrEqual(10); });
<>toBeLessThanOrEqual

Check whether the received value is less than or equal to the expected value .
// toBeLessThanOrEqual test(" test 10 Is it less than or equal to 10", () => { expect(10).
toBeLessThanOrEqual(10); });
<>toBeCloseTo

Check whether the floating-point number is close ( Is it approximately equal ).
// toBeCloseTo test(" test 0.1 + 0.2 Is it equal to 0.3", () => { expect(0.1 + 0.2).toBe(0.3
); // Fail expect(0.1 + 0.2).toBeCloseTo(0.3); // adopt });
<>toMatch

Check whether the value matches the string or regular .
// toMatch test(" Test whether the string contains baidu", () => { expect("www.baidu.com").toMatch(
"baidu"); expect("www.baidu.com").toMatch(/baidu/); });
<>toContain

Check whether the array contains an item ( be similar to js Medium includes method ).
// toContain test(" test list Whether it contains 3", () => { const list = [1, 2, 3]; expect(
list).toContain(3); });
<>toThrow

Test whether there is an exception thrown when calling the function .
// toThrow const fn1 = () => { console.log("hello"); }; const fn2 = () => {
throw newError("this is a new err"); }; test(" test fn1,fn2 Whether there is an exception when calling ", () => {
expect(fn1).toThrow(); // Fail expect(fn2).toThrow(); // adopt });
stay Jest
in , In addition to the common matchers listed above , There are many matchers for us . No need to remember all , Just know the common ones . If you want to know more , You can click here to view the official documents .

Technology