home ~ projects ~ socials

jest test async code

# Notes

- Add async to the function that gets passed to the test - Use await when calling the function under test - Make the function under test `async` - Add await as necessary in the function under test

# Test file

const sum = require('./sum')

test('adds 1 + 2 to equal 3', async () => {
  const result = await sum(1, 2)
  expect(result).toBe(3)
})

# File under test

async function sum(a, b) {
  // do some 'await' stuff here
  return a + b
}

module.exports = sum
-- end of line --