test(name, fn, timeout)
Also under the alias: it(name, fn, timeout)
All you need in a test file is the test
method which runs a test. For example, let’s say there’s a function inchesOfRain()
that should be zero. Your whole test could be:
test('did not rain', () => {
expect(inchesOfRain()).toBe(0);
});
The first argument is the test name; the second argument is a function that contains the expectations to test. The third argument (optional) is timeout
(in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds.
Note: If a promise is returned from
test
, Jest will wait for the promise to resolve before letting the test complete. Jest will also wait if you provide an argument to the test function, usually calleddone
. This could be handy when you want to test callbacks. See how to test async code here.
For example, let’s say fetchBeverageList()
returns a promise that is supposed to resolve to a list that has lemon
in it. You can test this with:
test('has lemon in it', () => {
return fetchBeverageList().then(list => {
expect(list).toContain('lemon');
});
});
Even though the call to test
will return right away, the test doesn’t complete until the promise resolves as well.