-
Использование синтаксиса
async/await
:test('example test', async () => { const response = await fetch('https://api.example.com/data'); const data = await response.json(); expect(data).toEqual(/* expected value */); });
-
Использование обещаний:
test('example test', () => { return fetch('https://api.example.com/data') .then(response => response.json()) .then(data => { expect(data).toEqual(/* expected value */); }); });
-
Использование
async
иdone
:test('example test', (done) => { fetch('https://api.example.com/data') .then(response => response.json()) .then(data => { expect(data).toEqual(/* expected value */); done(); }) .catch(error => { done.fail(error); }); });
-
Использование
async/await
сresolves
:test('example test', async () => { await expect(fetch('https://api.example.com/data')).resolves.toEqual(/* expected value */); });
-
Использование
async/await
сrejects
:test('example test', async () => { await expect(fetch('https://api.example.com/data')).rejects.toThrow(/* expected error */); });