Complete the code to declare an async function.
async function fetchData() {
const data = await [1]();
return data;
}The await keyword waits for the promise returned by getData() to resolve.
Complete the code to test an async function using Jest.
test('fetchData returns data', async () => { const data = await fetchData(); expect(data).[1]('value'); });
toBe for object comparison causes test failures.toEqual checks that the returned data matches the expected value deeply.
Fix the error in the async test by completing the code.
test('fetchData throws error', async () => { await expect(fetchData()).[1](); });
resolves when expecting an error causes false positives.Use rejects.toThrow to test that a promise rejects with an error.
Fill both blanks to correctly mock an async function with Jest.
jest.mock('./api'); const api = require('./api'); api.fetchData = jest.fn().[1](() => Promise.[2]('mocked data'));
mockRejectedValue when expecting success.mockImplementation without returning a promise.mockImplementation with Promise.resolve mocks a promise that resolves with the given value.
Fill all three blanks to write a complete async test with setup and teardown.
beforeEach(() => {
jest.[1]();
});
afterEach(() => {
jest.[2]();
});
test('async fetchData test', async () => {
const data = await fetchData();
expect(data).[3]('result');
});toBe when checking for substring presence.clearAllMocks clears mock calls before each test, resetAllMocks resets mock states after each test, and toContain checks if the result includes a value.