@morpho-org/morpho-test 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +80 -0
- package/src/asserts.ts +32 -0
- package/src/chai.ts +118 -0
- package/src/fixtures.ts +10 -0
- package/src/hardhat.ts +88 -0
- package/src/index.ts +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@morpho-org/morpho-test",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"files": [
|
|
6
|
+
"src"
|
|
7
|
+
],
|
|
8
|
+
"author": "Morpho Association <contact@morpho.org>",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prepublish": "yarn build",
|
|
12
|
+
"add:morpho-test": "yarn add",
|
|
13
|
+
"build": "tsc --build tsconfig.build.json",
|
|
14
|
+
"build:morpho-test": "yarn build",
|
|
15
|
+
"test": "jest",
|
|
16
|
+
"test:morpho-test": "yarn test"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
|
|
20
|
+
"@nomicfoundation/hardhat-ethers": "^3.0.4",
|
|
21
|
+
"@nomicfoundation/hardhat-network-helpers": "^1.0.9",
|
|
22
|
+
"chai": "^4.3.10",
|
|
23
|
+
"chai-as-promised": "^7.1.2",
|
|
24
|
+
"chai-exclude": "^2.1.0",
|
|
25
|
+
"ethers": "^6.12.1",
|
|
26
|
+
"ethers-types": "^3.16.0",
|
|
27
|
+
"hardhat": "^2.20.1",
|
|
28
|
+
"hardhat-deal": "^3.0.4",
|
|
29
|
+
"hardhat-tracer": "^2.8.2",
|
|
30
|
+
"jest": "^29.6.2",
|
|
31
|
+
"rxjs": "^7.8.1",
|
|
32
|
+
"sinon": "^17.0.1",
|
|
33
|
+
"sinon-chai": "^3.7.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/chai": "^4.3.14",
|
|
37
|
+
"@types/chai-as-promised": "^7",
|
|
38
|
+
"@types/jest": "^29.5.12",
|
|
39
|
+
"@types/mocha": "^10.0.6",
|
|
40
|
+
"@types/node": "^20.12.8",
|
|
41
|
+
"@types/sinon": "^17.0.3",
|
|
42
|
+
"@types/sinon-chai": "^3.2.12",
|
|
43
|
+
"dotenv": "^16.3.1",
|
|
44
|
+
"mocha": "^10.4.0",
|
|
45
|
+
"ts-jest": "^29.1.1",
|
|
46
|
+
"ts-node": "^10.9.2",
|
|
47
|
+
"typescript": "^5.4.5"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"rxjs": "^7.8.1"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"rxjs": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"jest": {
|
|
61
|
+
"verbose": true,
|
|
62
|
+
"testTimeout": 15000,
|
|
63
|
+
"maxWorkers": 1,
|
|
64
|
+
"transform": {
|
|
65
|
+
"^.+\\.tsx?$": [
|
|
66
|
+
"ts-jest",
|
|
67
|
+
{
|
|
68
|
+
"tsconfig": "tsconfig.json"
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
"testRegex": "(/src/.*|(\\.|/)(test|spec)+)\\.test\\.(jsx?|tsx?)$",
|
|
73
|
+
"moduleFileExtensions": [
|
|
74
|
+
"js",
|
|
75
|
+
"ts"
|
|
76
|
+
],
|
|
77
|
+
"preset": "ts-jest"
|
|
78
|
+
},
|
|
79
|
+
"gitHead": "858deb96bf68fca64ab6deaaf2568b9c765f310a"
|
|
80
|
+
}
|
package/src/asserts.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { parseEther } from "ethers";
|
|
3
|
+
|
|
4
|
+
export const assertApproxEqAbs = (
|
|
5
|
+
actual: bigint,
|
|
6
|
+
expected: bigint,
|
|
7
|
+
tolerance = 0n,
|
|
8
|
+
message?: string
|
|
9
|
+
) =>
|
|
10
|
+
expect(
|
|
11
|
+
actual >= expected ? actual - expected : expected - actual
|
|
12
|
+
).to.be.lessThanOrEqual(
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
tolerance,
|
|
15
|
+
message
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export const assertApproxEqRel = (
|
|
19
|
+
actual: bigint,
|
|
20
|
+
expected: bigint,
|
|
21
|
+
tolerance = 0n,
|
|
22
|
+
message?: string
|
|
23
|
+
) =>
|
|
24
|
+
assertApproxEqAbs(
|
|
25
|
+
actual,
|
|
26
|
+
expected,
|
|
27
|
+
(expected * tolerance) / parseEther("1"),
|
|
28
|
+
message
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
export const assertEq = (actual: bigint, expected: bigint) =>
|
|
32
|
+
assertApproxEqAbs(actual, expected, 0n);
|
package/src/chai.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import chai, { Assertion } from "chai";
|
|
2
|
+
import "chai-as-promised";
|
|
3
|
+
import chaiExclude from "chai-exclude";
|
|
4
|
+
import sinonChai from "sinon-chai";
|
|
5
|
+
|
|
6
|
+
chai.use(sinonChai);
|
|
7
|
+
chai.use(chaiExclude);
|
|
8
|
+
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
|
|
11
|
+
BigInt.prototype.toJSON = function () {
|
|
12
|
+
return this.toString();
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Extend Chai's Assertion interface to include the custom method
|
|
16
|
+
declare global {
|
|
17
|
+
namespace Chai {
|
|
18
|
+
interface ExpectStatic {
|
|
19
|
+
any: any;
|
|
20
|
+
anything: any;
|
|
21
|
+
number: any;
|
|
22
|
+
string: any;
|
|
23
|
+
bigint: any;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ANY = new String("[any value]");
|
|
29
|
+
const ANYTHING = new String("[any defined value]");
|
|
30
|
+
const NUMBER = new String("[any number]");
|
|
31
|
+
const STRING = new String("[any string]");
|
|
32
|
+
const BIGINT = new String("[any bigint]");
|
|
33
|
+
|
|
34
|
+
Object.defineProperty(chai.expect, "any", {
|
|
35
|
+
get: function () {
|
|
36
|
+
return ANY;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(chai.expect, "anything", {
|
|
40
|
+
get: function () {
|
|
41
|
+
return ANYTHING;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(chai.expect, "number", {
|
|
45
|
+
get: function () {
|
|
46
|
+
return NUMBER;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(chai.expect, "string", {
|
|
50
|
+
get: function () {
|
|
51
|
+
return STRING;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(chai.expect, "bigint", {
|
|
55
|
+
get: function () {
|
|
56
|
+
return BIGINT;
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function customEqual(obj1: any, obj2: any): boolean {
|
|
61
|
+
if (Object(obj1) !== obj1 || Object(obj2) !== obj2) {
|
|
62
|
+
return obj1 === obj2;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const keys1 = Object.keys(obj1);
|
|
66
|
+
const keys2 = Object.keys(obj2);
|
|
67
|
+
|
|
68
|
+
if (keys1.length !== keys2.length) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (let key of keys1) {
|
|
73
|
+
switch (obj2[key]) {
|
|
74
|
+
case ANY: {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case ANYTHING: {
|
|
78
|
+
if (obj1[key] == undefined) return false;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case STRING: {
|
|
82
|
+
if (typeof obj1[key] !== "string") return false;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case NUMBER: {
|
|
86
|
+
if (typeof obj1[key] !== "number") return false;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case BIGINT: {
|
|
90
|
+
if (typeof obj1[key] !== "bigint") return false;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
default: {
|
|
94
|
+
if (!customEqual(obj1[key], obj2[key])) return false;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
Assertion.overwriteMethod("eql", function (_super) {
|
|
104
|
+
return function (this: Chai.AssertionStatic, arg: any) {
|
|
105
|
+
if (typeof arg === "object" && typeof this._obj === "object") {
|
|
106
|
+
const result = customEqual(this._obj, arg);
|
|
107
|
+
this.assert(
|
|
108
|
+
result,
|
|
109
|
+
"expected #{this} to deeply equal #{exp}",
|
|
110
|
+
"expected #{this} not to deeply equal #{exp}",
|
|
111
|
+
arg,
|
|
112
|
+
this._obj
|
|
113
|
+
);
|
|
114
|
+
} else {
|
|
115
|
+
_super.apply(this, arguments);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
});
|
package/src/fixtures.ts
ADDED
package/src/hardhat.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Block } from "ethers";
|
|
2
|
+
import { HardhatRuntimeEnvironment } from "hardhat/types";
|
|
3
|
+
import sinon from "sinon";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
SnapshotRestorer,
|
|
7
|
+
mine as hardhatMine,
|
|
8
|
+
reset as hardhatReset,
|
|
9
|
+
takeSnapshot,
|
|
10
|
+
} from "@nomicfoundation/hardhat-network-helpers";
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
import("@nomicfoundation/hardhat-ethers");
|
|
14
|
+
|
|
15
|
+
const { ethers } = require("hardhat") as HardhatRuntimeEnvironment;
|
|
16
|
+
|
|
17
|
+
Object.defineProperty(ethers.provider, "_networkName", {
|
|
18
|
+
value: "mainnet",
|
|
19
|
+
});
|
|
20
|
+
} catch {}
|
|
21
|
+
|
|
22
|
+
export const mine = async (
|
|
23
|
+
blocks = 1,
|
|
24
|
+
options: { interval?: number } = {},
|
|
25
|
+
emit = true
|
|
26
|
+
) => {
|
|
27
|
+
const { ethers } = require("hardhat") as HardhatRuntimeEnvironment;
|
|
28
|
+
|
|
29
|
+
await hardhatMine(blocks, options);
|
|
30
|
+
|
|
31
|
+
if (emit)
|
|
32
|
+
await ethers.provider.emit("block", await ethers.provider.getBlockNumber());
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const reset = ({
|
|
36
|
+
url,
|
|
37
|
+
blockNumber,
|
|
38
|
+
}: {
|
|
39
|
+
url?: string | null;
|
|
40
|
+
blockNumber?: number;
|
|
41
|
+
} = {}) => {
|
|
42
|
+
if (url === null) return hardhatReset();
|
|
43
|
+
|
|
44
|
+
const { config } = require("hardhat") as HardhatRuntimeEnvironment;
|
|
45
|
+
|
|
46
|
+
url ??= config.networks.hardhat.forking?.url;
|
|
47
|
+
blockNumber ??= config.networks.hardhat.forking?.blockNumber;
|
|
48
|
+
|
|
49
|
+
return hardhatReset(url, blockNumber);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const resetAfterEach = (forking?: {
|
|
53
|
+
url?: string | null;
|
|
54
|
+
blockNumber?: number;
|
|
55
|
+
}) => {
|
|
56
|
+
afterEach(async () => {
|
|
57
|
+
await reset(forking);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const setUp = (onBefore?: (block: Block) => void) => {
|
|
62
|
+
const { ethers } = require("hardhat") as HardhatRuntimeEnvironment;
|
|
63
|
+
|
|
64
|
+
let snapshot: SnapshotRestorer;
|
|
65
|
+
|
|
66
|
+
before(async () => {
|
|
67
|
+
const block = await ethers.provider.getBlock("latest", false);
|
|
68
|
+
|
|
69
|
+
await onBefore?.(block!);
|
|
70
|
+
|
|
71
|
+
snapshot = await takeSnapshot();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const dataObserver = { next: sinon.spy(), error: sinon.spy() };
|
|
75
|
+
|
|
76
|
+
afterEach(async () => {
|
|
77
|
+
await snapshot.restore();
|
|
78
|
+
|
|
79
|
+
dataObserver.next.resetHistory();
|
|
80
|
+
dataObserver.error.resetHistory();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
after(async () => {
|
|
84
|
+
await reset();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return dataObserver;
|
|
88
|
+
};
|