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
:
test('object assignment', () => {
const data = {one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});
toEqual
recursively checks every field of an object or array.
tip
You can also test for the opposite of a matcher using not
:
test('adding positive numbers is not zero', () => {
for (let a = 1; a < 10; a++) {
for (let b = 1; b < 10; b++) {
expect(a + b).not.toBe(0);
}
}
});
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 onlynull
- matches only
undefined
toBeDefined
is the opposite oftoBeUndefined
toBeFalsy
matches anything that anif
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.
test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);
// toBe and toEqual are equivalent for numbers
expect(value).toBe(4);
expect(value).toEqual(4);
});
test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
//expect(value).toBe(0.3); This won't work because of rounding error
expect(value).toBeCloseTo(0.3); // This works.
});
Strings
You can check strings against regular expressions with toMatch
:
You can check if an array or iterable contains a particular item using toContain
:
'diapers',
'kleenex',
'trash bags',
'paper towels',
];
test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});
Exceptions
If you want to test whether a particular function throws an error when it’s called, use toThrow
.
function compileAndroidCode() {
throw new Error('you are using the wrong JDK!');
}
test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);
// You can also use a string that must be contained in the error message or a regexp
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
expect(() => compileAndroidCode()).toThrow(/JDK/);
// Or you can match an exact error message using a regexp like below
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
tip
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 .