Challenge - 5 Problems
Async Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this async test with Jest?
Consider this Jest test code that tests an async function. What will the test output be?
Node.js
test('async test with done callback', done => { setTimeout(() => { expect(2 + 2).toBe(4); done(); }, 100); });
Attempts:
2 left
💡 Hint
Look at how done() is used to signal test completion.
✗ Incorrect
The test uses the done callback correctly. The assertion runs after 100ms, then done() signals Jest to finish the test, so it passes.
📝 Syntax
intermediate2:00remaining
Which option correctly tests an async function returning a promise in Jest?
You want to test this async function that returns a promise. Which test code is correct?
Node.js
async function fetchData() { return 'data'; }
Attempts:
2 left
💡 Hint
Remember to await the promise or return it for Jest to handle async.
✗ Incorrect
Option A uses async/await correctly to wait for the promise to resolve before asserting.
🔧 Debug
advanced2:00remaining
Why does this Jest async test fail with a timeout?
Examine this test code. Why does Jest report a timeout error?
Node.js
test('async test missing return', () => { fetchData().then(data => { expect(data).toBe('data'); }); }); async function fetchData() { return 'data'; }
Attempts:
2 left
💡 Hint
Jest needs to know when async work finishes.
✗ Incorrect
Jest requires the test to return the promise or use async/await to wait. Without returning, Jest finishes test immediately causing timeout.
❓ state_output
advanced2:00remaining
What is the value of count after this async test runs?
Given this code, what is the final value of count after the test completes?
Node.js
let count = 0; function incrementAsync() { return new Promise(resolve => { setTimeout(() => { count += 1; resolve(count); }, 50); }); } test('incrementAsync increments count', async () => { await incrementAsync(); await incrementAsync(); });
Attempts:
2 left
💡 Hint
Each call increments count by 1 after 50ms.
✗ Incorrect
Each awaited call increments count by 1 sequentially, so after two calls count is 2.
🧠 Conceptual
expert3:00remaining
Which Jest test pattern ensures proper async error handling?
You want to test that an async function throws an error. Which test pattern correctly catches the error?
Node.js
async function failAsync() { throw new Error('fail'); }
Attempts:
2 left
💡 Hint
Use Jest's rejects matcher for promises that throw.
✗ Incorrect
Option A uses Jest's built-in rejects matcher to properly test async errors. Others either miss returning or do not await properly.