'Jest done callback is not assignable to parameter of type ProvidesCallback or undefined

I am trying to create a test with jest and I want to use done() callback but Typescript is not accepting it, I tried to use type any, jest.DoneCallback or leaving it without any type but again is not working. Any solution or idea?

Screenshot of error

it('implements optimistic concurrency control', async (done: any) => {
  const ticket = Ticket.build({
    title: 'Concert 123423',
    price: 5,
    userId: '123'
  });
  await ticket.save();
  
  const firstInstance = await Ticket.findById(ticket.id);
  const secondInstance = await Ticket.findById(ticket.id);

  firstInstance!.set({ price: 10 });
  secondInstance!.set({ price: 15 });

  await firstInstance!.save();

  try {
    await secondInstance!.save();
  } catch (err) {
    console.log(err);
    return done();
  }
  throw new Error('Should not reach this point');
});


Solution 1:[1]

You choose either to use Promise or a callback. In your case, you want to use async/await because you are waiting for the save() methods before continuing in the flow.

Your code updated:

it('implements optimistic concurrency control', async () => {
  const ticket = Ticket.build({
    title: 'Concert 123423',
    price: 5,
    userId: '123'
  });
  await ticket.save();
  
  const firstInstance = await Ticket.findById(ticket.id);
  const secondInstance = await Ticket.findById(ticket.id);

  firstInstance!.set({ price: 10 });
  secondInstance!.set({ price: 15 });

  await firstInstance!.save();

  try {
    await secondInstance!.save();
  } catch (err) {
    console.log(err);
    return;
  }
  throw new Error('Should not reach this point');
});

Here is an example of using the callback:

it('some asynchronous test', (done) => {
  request(app)
    .get('/endpoint')
    .expect(200)
    .expect((res) => {
       // insert some code stuff here
    })
    // The callback function is passed in so it will call
    // it once it finishes the asynchronous code
    .end(done)
})

Also, you shouldn't have to specify the type for the callback, it should be inferred. You can if you want though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Toomuchrice4u