0

I am trying to find a solution for tracking Promises.

The project that I am working on has some dangling async tasks that are not awaited/yielded. I am trying to find such cases, as these dangling calls are interfering with test suites.

One of my approaches was to spy on global Promise constructor with SinonJS spies. But while wrapping constructor, properties of Promise object gets hidden/overwritten by spy, rendering Promises unusable.

const spier = sinon.spy(global, 'Promise')

Perhaps there is some global tracking that I could exploit (such as event loop, or common array of live Promises).

Or maybe somebody has a bit more insight into Promises and can recommend alternative spying point on accessible internal Promise functions.

Would like to hear If you had any similar needs and your approaches.

Herman
  • 191
  • 1
  • 5

1 Answers1

1

You can monkey patch the promise constructor like this:

const global = window; // (in browser...)
const OldPromise = global.Promise; 
global.Promise = class Promise extends OldPromise {
    constructor(executor) {
    // do whatever you want here, but must call super()
    console.log('hello, promise');

    super(executor); // call native Promise constructor
  }
};

Promise.resolve();

Source: Monkey-patch Promise constructor

Jason Ehmke
  • 115
  • 1
  • 2
  • 9