typescript-book
typescript-book copied to clipboard
Jest async example needs an await https://basarat.gitbook.io/typescript/intro-1/jest
https://basarat.gitbook.io/typescript/intro-1/jest
Following is wrong:
test('basic',async () => {
expect(sum()).toBe(0);
});
test('basic again', async () => {
expect(sum(1, 2)).toBe(3);
}, 1000 /* optional timeout */);
You need await like:
test('basic', async () => {
expect(await sum()).toBe(0);
});
I think these async examples look weird and may not pass recommended lint checks, but I think they are "correct".
By my reading, these examples return a promise (because of async) and Jest will automatically wait for that Promise to settle.
Note:
If the arrow function's body consists of a single expression, you can omit the return keyword and the curly braces {}. The result of that single expression will be implicitly returned.