5

I'm using a testing setup with Mocha.js and a lot of promises within the tests. The tests depend on setting up stuff in the DOM, and between tests, the DOM is cleared. However, sometimes the tests run slowly and time out. In this case, their promises continue to execute but the DOM is cleared before the next test, so the promise may incorrectly throw errors into the next test. Is there a way to cancel or destroy all outstanding promises in-between tests? We are using when.js promises.

dandelion
  • 1,742
  • 1
  • 15
  • 18

1 Answers1

1

when.js supports a cancel() method. You could call it from a afterEach or after block in mocha. You might need to create an array at the top of each mocha file (or as global) to track your outstanding promises.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • It seems that this approach has two key limitations: – dandelion Dec 10 '13 at 00:25
  • Sorry, thought enter would make a linebreak and above comment went in prematurely. This seems like a workable approach, the key would be to have the DOM cleanup occur beforeEach test, os that the promise onReject handler in afterEach still can access the expected state. The drawback is that the test writers need to add every promise to the global tracked-promises array, which they might forget to do. Also, if the promises are generated elsewhere in the codebase, it may be difficult to add them to the array. – dandelion Dec 10 '13 at 00:33