0

I am trying to implement mocha testing, however it doesn't seem to work the way I expect it to. My code looks like the following:

async function getFoo() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved')
    }, 2500)
  })
}

describe(`Testing stuff`, function () {
  it('resolves with foo', () => {
    return getFoo().then(result => {
      assert.equal(result, 'foo')
    })
  })
})

Error message:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

To me it seems like I have to increase the threshold of the timeout. Is there a better way to do this? Since I don't know how long each test might take. I am testing nodejs child processes

best regards

pgalle
  • 216
  • 3
  • 13
  • Does this answer your question? [Change default timeout for mocha](https://stackoverflow.com/questions/23492043/change-default-timeout-for-mocha) – GOTO 0 Apr 09 '22 at 17:08

1 Answers1

1

As it is described in the error you need to use done() end of your test.

function delay(timeout = 2500) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved')
    }, timeout)
  })
}

describe(`Testing stuff`, function (done) {
  it('resolves with foo', (done) => {
    return delay().then(result => {
      assert.equal(result, 'foo') 
      done()
    })
  })
});

// better readability

describe(`Testing stuff`, function (done) {
  it('resolves with foo', (done) => {
    const result = await delay();
    assert.equal(result, 'foo') 
    done();
  })
});

But as you described you are testing child_processes which I don't recommend. Because to test child_process you need to promisify the child_process which doesn't make sense. Check here for more info How to promisify Node's child_process.exec and child_process.execFile functions with Bluebird?

If you really want to test child_process maybe you can mock the process with the link i gave but only for testing purposes.

hurricane
  • 6,521
  • 2
  • 34
  • 44