Using Matchers

    The simplest way to test a value is with exact equality.

    In this code, 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 Object.is to test exact equality. If you want to check the value of an object, use toEqual:

    1. test('object assignment', () => {
    2. const data = {one: 1};
    3. data['two'] = 2;
    4. expect(data).toEqual({one: 1, two: 2});
    5. });

    toEqual recursively checks every field of an object or array.

    tip

    You can also test for the opposite of a matcher using not:

    1. test('adding positive numbers is not zero', () => {
    2. for (let a = 1; a < 10; a++) {
    3. for (let b = 1; b < 10; b++) {
    4. expect(a + b).not.toBe(0);
    5. }
    6. }
    7. });

    Truthiness

    In tests, you sometimes need to distinguish between undefined, null, and false, but you sometimes do not want to treat these differently. Jest contains helpers that let you be explicit about what you want.

    • toBeNull matches only null
    • matches only undefined
    • toBeDefined is the opposite of toBeUndefined
    • toBeFalsy matches anything that an if statement treats as false

    For example:

    You should use the matcher that most precisely corresponds to what you want your code to be doing.

    Most ways of comparing numbers have matcher equivalents.

    1. test('two plus two', () => {
    2. const value = 2 + 2;
    3. expect(value).toBeGreaterThan(3);
    4. expect(value).toBeGreaterThanOrEqual(3.5);
    5. expect(value).toBeLessThan(5);
    6. expect(value).toBeLessThanOrEqual(4.5);
    7. // toBe and toEqual are equivalent for numbers
    8. expect(value).toBe(4);
    9. expect(value).toEqual(4);
    10. });
    1. test('adding floating point numbers', () => {
    2. const value = 0.1 + 0.2;
    3. //expect(value).toBe(0.3); This won't work because of rounding error
    4. expect(value).toBeCloseTo(0.3); // This works.
    5. });

    Strings

    You can check strings against regular expressions with toMatch:

    You can check if an array or iterable contains a particular item using toContain:

    1. 'diapers',
    2. 'kleenex',
    3. 'trash bags',
    4. 'paper towels',
    5. ];
    6. test('the shopping list has milk on it', () => {
    7. expect(shoppingList).toContain('milk');
    8. expect(new Set(shoppingList)).toContain('milk');
    9. });

    Exceptions

    If you want to test whether a particular function throws an error when it’s called, use toThrow.

    1. function compileAndroidCode() {
    2. throw new Error('you are using the wrong JDK!');
    3. }
    4. test('compiling android goes as expected', () => {
    5. expect(() => compileAndroidCode()).toThrow();
    6. expect(() => compileAndroidCode()).toThrow(Error);
    7. // You can also use a string that must be contained in the error message or a regexp
    8. expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
    9. expect(() => compileAndroidCode()).toThrow(/JDK/);
    10. // Or you can match an exact error message using a regexp like below
    11. expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
    12. expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass

    Using Matchers - 图2tip

    The function that throws an exception needs to be invoked within a wrapping function otherwise the toThrow assertion will fail.

    Once you’ve learned about the matchers that are available, a good next step is to check out how Jest lets you .