0

I have a situation where the test needs to wait for a minute to execute. Tried below code but it doesn't work:

describe('/incidents/:incidentId/feedback', async function feedback() {
  it('creates and update', async function updateIncident() {
    // this works fine
  });

  // need to wait here for a minute before executing below test
  it('check incident has no feedback', function checkFeedback(done){
      setTimeout(function(){
        const result = send({
          user: 'Acme User',
          url: `/incidents/${createdIncident.id}/feedback`,
          method: 'get',
        });
        console.log(result);
        expect(result.response.statusCode).to.equal(200);
        expect(result.response.hasFeedback).to.equal(false);
        done();
      }, 1000*60*1);
  });
});

Here, send() returns Promise. I tried with async await but it didn't work.

How do I make test wait for a minute before executing ?

Valay
  • 1,991
  • 2
  • 43
  • 98
  • If you are waiting on something to be updated from another test, you're doing it wrong. – James Nov 22 '18 at 16:28

1 Answers1

3

If promises are used, they preferably shouldn't be mixed with plain callbacks.

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

...

  it('check incident has no feedback', async function checkFeedback(){
    this.timeout(1.33 * 60 * 1000);

    await wait(1 * 60 * 1000);
    const result = await send(...);
    ...
  });
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Tried this. it throws an error `Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.`. – Valay Nov 22 '18 at 16:04
  • 2
    This is a different issue. This means that you need to increase a timeout before using such long delays. See for reference, https://stackoverflow.com/questions/15971167/how-to-increase-timeout-for-a-single-test-case-in-mocha – Estus Flask Nov 22 '18 at 16:22