Test the Hidden Promise Using Done or Await

Understand how to test methods that do not expose the Promise, using either the Done function or async and await.

In everyday work, we often need to deal with Promises (MDN article). If the function we test is async (it returns a Promise), we can use async and await in our tests.

When the function is not returning a Promise and not using the async keyword but still relies on a Promise, we can’t use Jasmine’s asynchronous testing capabilities directly. There are two approaches for testing such functions/methods that hide their instances of Promise:

  • The Done function from Jasmine.
  • The async and await keywords with an assisting Promise.

Let’s start with the class that has this hidden Promise method: - the ArticleDelete class.

The hidden Promise

What follows is a very simplified example of a class that handles deleting articles in a browser application. It accepts some input and makes a request to a server API if conditions are met.

For this lesson, the delete method is not asynchronous, but it still uses the Promise returning ArticleAPI and works with .then and .catch callbacks (click for information on Promise chain flow). The Promise is not exposed; so, it’s hidden.

Run the code playground below and see the breakdown below.

Get hands-on with 1400+ tech skills courses.