I have this piece of code:
if (this.isRequestPending) {
this.conversationsAbortController.abort()
this.conversationsAbortController = new AbortController()
}
I'm already able to catch the call to .abort() using jest.spyOn(AbortController.prototype, 'abort') but I'd like to also catch the new AbortController().
I've tried:
jest.spyOn(AbortController.prototype, 'new')jest.spyOn(AbortController.prototype, 'constructor')jest.spyOn(AbortController.prototype, 'AbortController')But it doesn't seem to work.
I did look at the global.d.ts file for this:
interface AbortController {
/**
* Returns the AbortSignal object associated with this object.
*/
readonly signal: AbortSignal;
/**
* Invoking this method will set this object's AbortSignal's aborted flag and signal to any observers that the associated activity is to be aborted.
*/
abort(): void;
}
/** A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object. */
interface AbortSignal {
/**
* Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
*/
readonly aborted: boolean;
}
declare var AbortController: {
prototype: AbortController;
new(): AbortController;
};
declare var AbortSignal: {
prototype: AbortSignal;
new(): AbortSignal;
// TODO: Add abort() static
};
But I'm not sure what to make of it.