Create a jest test to fail an async function which does not use await

Viewed 32
async function isContentUser (userEmail, studentStatus) {
  const hasContentAccess = getContentCreatorAccess()
  const hasContentStudentStatus = studentStatus === CONTENT

  return hasContentStudentStatus || hasContentAccess
}

async function getContentCreatorAccess () {
  const { contentCreatorAccess } = await api.getContentCreatorPermission()

  return contentCreatorAccess
}

As you can see in isContentUser function, we're not awaiting getContentCreateAccess, but the below existing test pass, because hasContentAccess in the return value is a promise and we're awaiting the promise in the test.

And I want to create a PR which adds await before getContentCreatorAccess call, how can I come up with a test which works with await but not in the current code.

  it(`should return true if studentStatus is not Content but user has content permission`, async () => {
    api.getContentCreatorPermission.mockResolvedValue({
      contentCreatorAccess: true
    })

    expect(await isContentUser('email@gmail.com', 'Audit')).toBe(true)
  })
0 Answers
Related