forked from 0xProject/0x-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchai_setup.ts
51 lines (49 loc) · 1.73 KB
/
chai_setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { RichRevertReason } from '@0x/order-utils';
import * as chai from 'chai';
import chaiAsPromised = require('chai-as-promised');
import ChaiBigNumber = require('chai-bignumber');
import * as dirtyChai from 'dirty-chai';
export const chaiSetup = {
configure(): void {
chai.config.includeStack = true;
chai.use(ChaiBigNumber());
chai.use(dirtyChai);
chai.use(chaiAsPromised);
chai.use(richRevertEquality);
},
};
// A chai utlity to match rich reverts.
function richRevertEquality(_chai: any): void {
// tslint:disable:only-arrow-functions
const Assertion = _chai.Assertion;
Assertion.overwriteMethod('rejectedWith', function(_super: (x: any) => Promise<void>): (b: any) => Promise<void> {
return async function(this: any, b: any | RichRevertReason): Promise<void> {
const p = this._obj;
let a;
try {
if (p instanceof Promise) {
await p;
this.assert(false, 'Expected promise to reject');
} else {
a = p;
}
} catch (err) {
a = err;
}
if (b instanceof RichRevertReason) {
// Try to decode the result as a rich revert reason.
if (a instanceof Buffer) {
const hex = a.toString('hex');
a = `0x${hex}`;
}
if (typeof a === 'string') {
a = RichRevertReason.decode(a);
}
if (a instanceof RichRevertReason) {
this.assert(a.equals(b), `${a} != ${b}`);
}
}
_super.call(this, b);
};
});
}