Skip to content

Unhandled promise rejection in test

Quick summary

Your test has an unhandled promise rejection — a promise was rejected but no .catch() or try/catch handled it.

Why this happens

  • An async operation rejects but the test does not await it or catch the rejection

  • A fire-and-forget promise is started in the test and rejects later, outside the test scope

  • An event handler or callback creates a promise that rejects without a catch handler

Minimal example

✗ Broken code
test('sends notification', () => {
  sendNotification('Hello'); // returns a promise, not awaited
  // if sendNotification rejects, unhandled rejection!
  expect(log).toContain('sent');
});
✓ Fixed code
test('sends notification', async () => {
  await sendNotification('Hello'); // rejection is caught by test
  expect(log).toContain('sent');
});

Awaiting the promise ensures that if it rejects, the test fails with a clear error instead of an unhandled rejection warning.

How to diagnose

  • Check for unawaited promises in the test that may reject after the test completes

  • Review the code under test for promise chains that lack a .catch() handler

  • Use process.on('unhandledRejection') to log the rejection source during tests

How to fix

  • Await all promises in the test so rejections are caught by the test framework

  • Add .catch() handlers to fire-and-forget promises or use Promise.all to await them

  • Fix the code under test to handle promise rejections properly with try/catch or .catch()

How to prevent

  • Always await promises in tests and enable lint rules that flag floating promises

  • Add global unhandled rejection handlers in the test setup to catch stray rejections

Related Resources

Related Glossary

Related Lessons

← Back to language