Search⌘ K
AI Features

Testing with Callbacks

Explore how to test asynchronous JavaScript functions that use callbacks with Jest. This lesson teaches using the done argument to properly handle asynchronous assertions, ensuring tests complete only after callbacks execute, enabling reliable testing of callback-based code.

The callback pattern

A callback is a function that is passed as the last argument to another function and called at the end of that function. It looks something like this:

function logger(str) {
  console.log(str);
}

function getUserName(user, callback) {
  const { name } = user;

 
...