0
0
Node.jsframework~20 mins

Testing async code in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
});
AThe test times out because done() is never called.
BThe test fails immediately due to missing await.
CThe test passes after 100ms delay.
DThe test throws a syntax error.
Attempts:
2 left
💡 Hint
Look at how done() is used to signal test completion.
📝 Syntax
intermediate
2: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';
}
A
test('fetchData returns data', async () => {
  const data = await fetchData();
  expect(data).toBe('data');
});
B
test('fetchData returns data', () => {
  const data = fetchData();
  expect(data).toBe('data');
});
C
test('fetchData returns data', () => {
  return fetchData().then(data => {
    expect(data).toBe('data');
  });
});
D
test('fetchData returns data', () => {
  expect(fetchData()).toBe('data');
});
Attempts:
2 left
💡 Hint
Remember to await the promise or return it for Jest to handle async.
🔧 Debug
advanced
2: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';
}
ABecause the test function is marked async but does not await.
BBecause fetchData throws an error that is not caught.
CBecause expect is used incorrectly inside then.
DBecause the test function does not return the promise, Jest does not wait for it.
Attempts:
2 left
💡 Hint
Jest needs to know when async work finishes.
state_output
advanced
2: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();
});
Acount is undefined
Bcount is 2
Ccount is 0
Dcount is 1
Attempts:
2 left
💡 Hint
Each call increments count by 1 after 50ms.
🧠 Conceptual
expert
3: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');
}
A
test('failAsync throws', async () => {
  await expect(failAsync()).rejects.toThrow('fail');
});
B
test('failAsync throws', async () => {
  try {
    await failAsync();
  } catch (e) {
    expect(e.message).toBe('fail');
  }
});
C
test('failAsync throws', () => {
  expect(() => failAsync()).toThrow('fail');
});
D
test('failAsync throws', () => {
  return failAsync().catch(e => {
    expect(e.message).toBe('fail');
  });
});
Attempts:
2 left
💡 Hint
Use Jest's rejects matcher for promises that throw.