typescript-book icon indicating copy to clipboard operation
typescript-book copied to clipboard

Jest async example needs an await https://basarat.gitbook.io/typescript/intro-1/jest

Open AnupamKhosla opened this issue 5 months ago • 1 comments

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);
});

AnupamKhosla avatar Jul 06 '25 12:07 AnupamKhosla

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.

joebowbeer avatar Jul 06 '25 18:07 joebowbeer