@0x/dev-utils: revertWith mocha extensions now accept Promise-like objects instead of just Promises.

This commit is contained in:
Lawrence Forman
2019-08-01 18:15:29 -04:00
parent 884b1add8e
commit 264b1d69d9
2 changed files with 18 additions and 2 deletions

View File

@@ -1,4 +1,13 @@
[ [
{
"version": "2.3.1",
"changes": [
{
"note": "`revertWith` mocha extensions now accept Promise-like objects instead of just Promises",
"pr": "TODO"
}
]
},
{ {
"version": "2.3.0", "version": "2.3.0",
"changes": [ "changes": [

View File

@@ -34,7 +34,7 @@ export function revertErrorHelper(_chai: Chai): void {
return async function(this: ChaiAssertionInstance, expected: any, ...rest: any[]): Promise<void> { return async function(this: ChaiAssertionInstance, expected: any, ...rest: any[]): Promise<void> {
const maybePromise = this._obj; const maybePromise = this._obj;
// Make sure we're working with a promise. // Make sure we're working with a promise.
chaiAssert(_chai, maybePromise instanceof Promise, `Expected ${maybePromise} to be a promise`); assertIsPromiseLike(_chai, maybePromise);
// Wait for the promise to reject. // Wait for the promise to reject.
let resolveValue; let resolveValue;
let rejectValue: any; let rejectValue: any;
@@ -58,7 +58,7 @@ export function revertErrorHelper(_chai: Chai): void {
return async function(this: ChaiAssertionInstance, expected: any, ...rest: any[]): Promise<void> { return async function(this: ChaiAssertionInstance, expected: any, ...rest: any[]): Promise<void> {
const maybePromise = this._obj; const maybePromise = this._obj;
// Make sure we're working with a promise. // Make sure we're working with a promise.
chaiAssert(_chai, maybePromise instanceof Promise, `Expected ${maybePromise} to be a promise`); assertIsPromiseLike(_chai, maybePromise);
// Wait for the promise to resolve. // Wait for the promise to resolve.
if (!compareRevertErrors.call(this, _chai, await maybePromise, expected)) { if (!compareRevertErrors.call(this, _chai, await maybePromise, expected)) {
// Wasn't handled by the comparison function so call the previous handler. // Wasn't handled by the comparison function so call the previous handler.
@@ -133,3 +133,10 @@ function chaiFail(_chai: Chai, failMessage?: string, expected?: any, actual?: an
const assert = new _chai.Assertion(); const assert = new _chai.Assertion();
assert.assert(false, failMessage, undefined, expected, actual); assert.assert(false, failMessage, undefined, expected, actual);
} }
function assertIsPromiseLike(_chai: Chai, maybePromise: any): void {
if (maybePromise.then instanceof Function && maybePromise.catch instanceof Function) {
return;
}
chaiFail(_chai, `Expected ${maybePromise} to be a promise`, new Promise(() => 1), maybePromise);
}