Finding ChunkyLover53: Understanding Asynchronous Testing with Mocha.

Kevin Gleeson
3 min readOct 31, 2020

I’m currently enjoying working through “The Complete Guide to Testing Javascript & Node Applications” Udemy course by Kati Frantz. While the first few lessons refreshed a lot of the basics I had already known about testing, it was the lesson on Asynchronous Testing that really got my attention. I’d like to go over a few ways Frantz describes how you can perform asynchronous tests in your applications.

We’ll find you, ChunkyLover53!!!!

Let’s say that we have an asynchronous function that finds a user by searching for their email. The function is asynchronous due to its Promise and a timeout function. Our goal here is for our test to pass when a user is present after the search. To do this, we’ll check the contents of our promise in a few different ways. If you’ve written a fetch request before, this sort of syntax might seem familiar.

The one and done( )

In this test approach we’re going to tap into the done function. All done is, is a callback function that tells our test runner that our tests are done running. This is a quick way to tell your code when an asynchronous function ends.

 1.   describe('The findUserByEmail function', () => {
2. it('finds a user by their email', done => {
3. findUserByEmail('ChunkyLover53@aol.com').then(response => { assert.equal(response.message, 'User found successfully.') done() }) }) })

In this case, after describing the initial test at 1, we tell our code at 2 that once the done function is invoked, any asynchronous stuff going on will have finished, and we’ll be able to see if our assertion was correct.

Bullet 3 is where things actually get tested. We tell our function to see if the message in our response is equal to “User found successfully.”. From there, the done function is invoked and our test ends.

The it( ) Crowd

We can simplify what we did above by focusing on what we do in the it( ) function. Instead of using the done function, we’re just going to keep this as a normal callback with an anonymous function. The goal of this test is to just return the promise and check the assertion after it’s resolved. We’ll call this the return promise method.

   describe ('The findUserByEmail function', () => {
it('finds a user by email (return promise method)', () => {

1. return findUserByEmail('ChunkyLover53@aol.com').then((response) => {
assert.equal(response.message, 'User found successfully.') }) })})

At 1, we are explicitly returning our function, then getting our response from our promise. Once we do that, all we need to do is create our assertion.

Nsync*, await( )

Terrible pun, I know I know

The last method we can use to test asynchronous functions is to use async/await. Await is the key to telling our asynchronous function to halt, and this keyword can only be used inside an async( ) function. In the method below, we are starting our async( ) function at 1. At 2, we’re creating a variable that will use await to wait for the promise to resolve before moving onto our assertion.

    describe ('The findUserByEmail function', () => {.             1.   it('finds a user by email (async/await)', async() => {2.   const response = await findUserByEmail('ChunkyLover53@aol.com')     assert.equal(response.message, 'User found successfully.')    })})

I hope these little ways to test async functions helped you become a better code tester! Now it’s time to work these tests into some of my projects!

--

--

Kevin Gleeson

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.