Andrew Gilliland

Testing Async Code

Last Updated: May 12, 2024

index.ts

const fetchData = (callback) => {
  setTimeout(() => {
    callback("peanut butter");
  }, 1000);
};

const fetchDataPromise = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("peanut butter");
    }, 1000);
  });
};

module.exports = fetchDataPromise;

index.test.js

const fetchPromise = require("./index");

// test("the data is peanut butter", () => {
//   return expect(fetchPromise()).resolves.toBe("peanut butter");
// });

// test("the data is not fetched", () => {
//   return expect(fetchPromise()).rejects.toThrow("error");
// });

test("the data is peanut butter", async () => {
  const data = await fetchPromise();
  expect(data).toBe("peanut butter");
});