The Jest Book


describe('how to test asynchronous code in Jest', () => {
  
  it('can use done() to signal when to end the test', (done) => {
    fetch(endpoint)
      .then((response) => response.json())
      .then((data) => { expect(data).toBeTruthy(); done() })
  })
  
  it('can return a Promise and Jest will handle it', () => {
    return fetch(endpoint)
      .then((response) => response.json())
      .then((data) => expect(data).toBeTruthy())
  })  
  
  it('can use async and await to pause appropriately', async () => {
    const response = await fetch(endpoint)
    const data = await response.json()
    expect(data).toBeTruthy()
  })
  
})

Last updated