Created
April 24, 2020 15:56
-
-
Save rcollette/51174165c96fb0d6ceeda35074f934ea to your computer and use it in GitHub Desktop.
A Typescript Class Used to Mock Promises in Unit Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable-next-line:no-any | |
export class PromiseResolver<T = any> { | |
private _resolve: (value?: T | PromiseLike<T>) => void; | |
// tslint:disable-next-line:no-any | |
private _reject: (reason?: any) => void; | |
// tslint:disable-next-line:no-any | |
public executor = (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => { | |
this._resolve = resolve; | |
this._reject = reject; | |
}; | |
public createPromise(): Promise<T> { | |
return new Promise<T>(this.executor); | |
} | |
public resolve(value?: T | PromiseLike<T>): void { | |
this._resolve(value); | |
} | |
// tslint:disable-next-line:no-any | |
public reject(reason?: any): void { | |
this._reject(reason); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done this way because this is not possible.