Skip to content

Commit 6739db6

Browse files
authored
Use Hardhat client and ethers in unit tests (#847)
1 parent c3c5615 commit 6739db6

15 files changed

+236
-762
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@nomiclabs/hardhat-truffle5": "^2.0.0",
4949
"@nomiclabs/hardhat-waffle": "^2.0.1",
5050
"@nomiclabs/hardhat-web3": "^2.0.0",
51-
"@truffle/contract": "4.0.36",
5251
"chai": "^4.3.4",
5352
"decache": "^4.5.1",
5453
"ethereum-waffle": "^3.4.0",

scripts/ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SILENT=true node --max-old-space-size=4096 \
99
-- \
1010
mocha \
1111
test/units/* test/integration/* \
12+
--require "test/util/mochaRootHook.js" \
1213
--timeout 100000 \
1314
--no-warnings \
1415
--exit \

scripts/unit.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ node --max-old-space-size=4096 \
66
--exclude '**/test/**/' \
77
-- \
88
mocha test/units/* \
9+
--require "test/util/mochaRootHook.js" \
910
--timeout 100000 \
1011
--no-warnings \
1112
--exit

test/units/assert.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
const assert = require('assert');
22
const util = require('./../util/util.js');
3-
4-
const client = require('ganache-cli');
53
const Coverage = require('./../../lib/coverage');
64
const Api = require('./../../lib/api')
75

86
describe('asserts and requires', () => {
97
let coverage;
108
let api;
119

12-
before(async () => {
13-
api = new Api({silent: true});
14-
await api.ganache(client);
15-
})
10+
before(async () => api = new Api({silent: true}));
1611
beforeEach(() => coverage = new Coverage());
1712
after(async() => await api.finish());
1813

1914
// Assert was covered as a branch up to v0.7.11. But since those
2015
// conditions are never meant to be fullfilled (and assert is really for smt)
2116
// people disliked this...
2217
it('should *not* cover assert statements as branches (pass)', async function() {
23-
const contract = await util.bootstrapCoverage('assert/Assert', api);
18+
const contract = await util.bootstrapCoverage('assert/Assert', api, this.provider);
2419
coverage.addContract(contract.instrumented, util.filePath);
25-
await contract.instance.a(true);
20+
await contract.instance.a(true, contract.gas);
2621
const mapping = coverage.generate(contract.data, util.pathPrefix);
2722

2823
assert.deepEqual(mapping[util.filePath].l, {
@@ -37,31 +32,29 @@ describe('asserts and requires', () => {
3732
});
3833
});
3934

40-
// NB: truffle/contract replays failing txs as .calls to obtain the revert reason from the return
41-
// data. Hence the 2X measurements.
4235
it('should *not* cover assert statements as branches (fail)', async function() {
43-
const contract = await util.bootstrapCoverage('assert/Assert', api);
36+
const contract = await util.bootstrapCoverage('assert/Assert', api, this.provider);
4437
coverage.addContract(contract.instrumented, util.filePath);
4538

46-
try { await contract.instance.a(false) } catch(err) { /* Invalid opcode */ }
39+
try { await contract.instance.a(false, contract.gas) } catch(err) { /* Invalid opcode */ }
4740

4841
const mapping = coverage.generate(contract.data, util.pathPrefix);
4942
assert.deepEqual(mapping[util.filePath].l, {
50-
5: 2,
43+
5: 1,
5144
});
5245
assert.deepEqual(mapping[util.filePath].b, {});
5346
assert.deepEqual(mapping[util.filePath].s, {
54-
1: 2,
47+
1: 1,
5548
});
5649
assert.deepEqual(mapping[util.filePath].f, {
57-
1: 2,
50+
1: 1,
5851
});
5952
});
6053

6154
it('should cover multi-line require stmts as `if` statements when they pass', async function() {
62-
const contract = await util.bootstrapCoverage('assert/RequireMultiline', api);
55+
const contract = await util.bootstrapCoverage('assert/RequireMultiline', api, this.provider);
6356
coverage.addContract(contract.instrumented, util.filePath);
64-
await contract.instance.a(true, true, true);
57+
await contract.instance.a(true, true, true, contract.gas);
6558
const mapping = coverage.generate(contract.data, util.pathPrefix);
6659

6760
assert.deepEqual(mapping[util.filePath].l, {
@@ -78,34 +71,32 @@ describe('asserts and requires', () => {
7871
});
7972
});
8073

81-
// NB: Truffle replays failing txs as .calls to obtain the revert reason from the return
82-
// data. Hence the 2X measurements.
8374
it('should cover multi-line require stmts as `if` statements when they fail', async function() {
84-
const contract = await util.bootstrapCoverage('assert/RequireMultiline', api);
75+
const contract = await util.bootstrapCoverage('assert/RequireMultiline', api, this.provider);
8576
coverage.addContract(contract.instrumented, util.filePath);
8677

87-
try { await contract.instance.a(true, true, false) } catch(err) { /* Revert */ }
78+
try { await contract.instance.a(true, true, false, contract.gas) } catch(err) { /* Revert */ }
8879

8980
const mapping = coverage.generate(contract.data, util.pathPrefix);
9081

9182
assert.deepEqual(mapping[util.filePath].l, {
92-
5: 2,
83+
5: 1,
9384
});
9485
assert.deepEqual(mapping[util.filePath].b, {
95-
1: [0, 2],
86+
1: [0, 1],
9687
});
9788
assert.deepEqual(mapping[util.filePath].s, {
98-
1: 2,
89+
1: 1,
9990
});
10091
assert.deepEqual(mapping[util.filePath].f, {
101-
1: 2,
92+
1: 1,
10293
});
10394
});
10495

10596
it('should cover require statements with method arguments', async function() {
106-
const contract = await util.bootstrapCoverage('assert/Require-fn', api);
97+
const contract = await util.bootstrapCoverage('assert/Require-fn', api, this.provider);
10798
coverage.addContract(contract.instrumented, util.filePath);
108-
await contract.instance.a(true);
99+
await contract.instance.a(true, contract.gas);
109100
const mapping = coverage.generate(contract.data, util.pathPrefix);
110101

111102
assert.deepEqual(mapping[util.filePath].l, {
@@ -123,9 +114,9 @@ describe('asserts and requires', () => {
123114
});
124115

125116
it('should cover require statements with method arguments & reason string', async function() {
126-
const contract = await util.bootstrapCoverage('assert/Require-fn-reason', api);
117+
const contract = await util.bootstrapCoverage('assert/Require-fn-reason', api, this.provider);
127118
coverage.addContract(contract.instrumented, util.filePath);
128-
await contract.instance.a(true);
119+
await contract.instance.a(true, contract.gas);
129120
const mapping = coverage.generate(contract.data, util.pathPrefix);
130121

131122
assert.deepEqual(mapping[util.filePath].l, {

test/units/conditional.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
const assert = require('assert');
22
const util = require('./../util/util.js');
3-
4-
const client = require('ganache-cli');
53
const Coverage = require('./../../lib/coverage');
64
const Api = require('./../../lib/api')
75

86
describe('ternary conditionals', () => {
97
let coverage;
108
let api;
119

12-
before(async () => {
13-
api = new Api({silent: true});
14-
await api.ganache(client);
15-
})
10+
before(async () => api = new Api({silent: true}));
1611
beforeEach(() => coverage = new Coverage());
1712
after(async() => await api.finish());
1813

19-
async function setupAndRun(solidityFile){
20-
const contract = await util.bootstrapCoverage(solidityFile, api);
14+
async function setupAndRun(solidityFile, provider){
15+
const contract = await util.bootstrapCoverage(solidityFile, api, provider);
2116
coverage.addContract(contract.instrumented, util.filePath);
22-
await contract.instance.a();
17+
await contract.instance.a(contract.gas);
2318
return coverage.generate(contract.data, util.pathPrefix);
2419
}
2520

2621
it('should cover a conditional that reaches the consequent (same-line)', async function() {
27-
const mapping = await setupAndRun('conditional/sameline-consequent');
22+
const mapping = await setupAndRun('conditional/sameline-consequent', this.provider);
2823

2924
assert.deepEqual(mapping[util.filePath].l, {
3025
5: 1, 6: 1, 7: 1,
@@ -41,7 +36,7 @@ describe('ternary conditionals', () => {
4136
});
4237

4338
it('should cover an unbracketed conditional that reaches the consequent (same-line)', async function() {
44-
const mapping = await setupAndRun('conditional/unbracketed-condition');
39+
const mapping = await setupAndRun('conditional/unbracketed-condition', this.provider);
4540

4641
assert.deepEqual(mapping[util.filePath].l, {
4742
5: 1, 6: 1, 7: 1,
@@ -58,7 +53,7 @@ describe('ternary conditionals', () => {
5853
});
5954

6055
it('should cover a multi-part conditional (&&) that reaches the consequent', async function() {
61-
const mapping = await setupAndRun('conditional/and-condition');
56+
const mapping = await setupAndRun('conditional/and-condition', this.provider);
6257

6358
assert.deepEqual(mapping[util.filePath].l, {
6459
5: 1, 6: 1, 7: 1,
@@ -75,7 +70,7 @@ describe('ternary conditionals', () => {
7570
});
7671

7772
it('should cover a multi-part conditional (||) that reaches the consequent', async function() {
78-
const mapping = await setupAndRun('conditional/or-condition');
73+
const mapping = await setupAndRun('conditional/or-condition', this.provider);
7974

8075
assert.deepEqual(mapping[util.filePath].l, {
8176
5: 1, 6: 1, 7: 1,
@@ -92,7 +87,7 @@ describe('ternary conditionals', () => {
9287
});
9388

9489
it('should cover a multi-part unbracketed conditional (||) that reaches the consequent', async function() {
95-
const mapping = await setupAndRun('conditional/unbracketed-or-condition');
90+
const mapping = await setupAndRun('conditional/unbracketed-or-condition', this.provider);
9691

9792
assert.deepEqual(mapping[util.filePath].l, {
9893
5: 1, 6: 1, 7: 1,
@@ -109,7 +104,7 @@ describe('ternary conditionals', () => {
109104
});
110105

111106
it('should cover an always-false multi-part unbracketed conditional (||)', async function() {
112-
const mapping = await setupAndRun('conditional/or-always-false-condition');
107+
const mapping = await setupAndRun('conditional/or-always-false-condition', this.provider);
113108

114109
assert.deepEqual(mapping[util.filePath].l, {
115110
5: 1, 6: 1, 7: 1,
@@ -126,7 +121,7 @@ describe('ternary conditionals', () => {
126121
});
127122

128123
it('should cover a conditional that reaches the alternate (same-line)', async function() {
129-
const mapping = await setupAndRun('conditional/sameline-alternate');
124+
const mapping = await setupAndRun('conditional/sameline-alternate', this.provider);
130125

131126
assert.deepEqual(mapping[util.filePath].l, {
132127
5: 1, 6: 1, 7: 1,
@@ -143,7 +138,7 @@ describe('ternary conditionals', () => {
143138
});
144139

145140
it('should cover a conditional that reaches the consequent (multi-line)', async function() {
146-
const mapping = await setupAndRun('conditional/multiline-consequent');
141+
const mapping = await setupAndRun('conditional/multiline-consequent', this.provider);
147142

148143
assert.deepEqual(mapping[util.filePath].l, {
149144
5: 1, 6: 1, 7: 1,
@@ -160,7 +155,7 @@ describe('ternary conditionals', () => {
160155
});
161156

162157
it('should cover a conditional that reaches the alternate (multi-line)', async function() {
163-
const mapping = await setupAndRun('conditional/multiline-alternate');
158+
const mapping = await setupAndRun('conditional/multiline-alternate', this.provider);
164159

165160
assert.deepEqual(mapping[util.filePath].l, {
166161
5: 1, 6: 1, 7: 1,
@@ -178,7 +173,7 @@ describe('ternary conditionals', () => {
178173

179174
// Runs bool z = (x) ? false : true;
180175
it('should cover a definition assignment by conditional that reaches the alternate', async function() {
181-
const mapping = await setupAndRun('conditional/declarative-exp-assignment-alternate');
176+
const mapping = await setupAndRun('conditional/declarative-exp-assignment-alternate', this.provider);
182177

183178
assert.deepEqual(mapping[util.filePath].l, {
184179
5: 1, 6: 1, 7: 1,
@@ -196,7 +191,7 @@ describe('ternary conditionals', () => {
196191

197192
// Runs z = (x) ? false : true;
198193
it('should cover an identifier assignment by conditional that reaches the alternate', async function() {
199-
const mapping = await setupAndRun('conditional/identifier-assignment-alternate');
194+
const mapping = await setupAndRun('conditional/identifier-assignment-alternate', this.provider);
200195

201196
assert.deepEqual(mapping[util.filePath].l, {
202197
5: 1, 6: 1, 7: 1, 8: 1,
@@ -213,7 +208,7 @@ describe('ternary conditionals', () => {
213208
});
214209

215210
it('should cover an assignment to a member expression (reaches the alternate)', async function() {
216-
const mapping = await setupAndRun('conditional/mapping-assignment');
211+
const mapping = await setupAndRun('conditional/mapping-assignment', this.provider);
217212

218213
assert.deepEqual(mapping[util.filePath].l, {
219214
11: 1, 12: 1,

test/units/function.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
const assert = require('assert');
22
const util = require('./../util/util.js');
3-
4-
const client = require('ganache-cli');
53
const Coverage = require('./../../lib/coverage');
64
const Api = require('./../../lib/api')
75

86
describe('function declarations', () => {
97
let coverage;
108
let api;
119

12-
before(async () => {
13-
api = new Api({silent: true});
14-
await api.ganache(client);
15-
})
10+
before(async () => api = new Api({silent: true}));
1611
beforeEach(() => coverage = new Coverage());
1712
after(async() => await api.finish());
1813

@@ -37,9 +32,9 @@ describe('function declarations', () => {
3732
});
3833

3934
it('should cover a simple invoked function call', async function() {
40-
const contract = await util.bootstrapCoverage('function/function-call', api);
35+
const contract = await util.bootstrapCoverage('function/function-call', api, this.provider);
4136
coverage.addContract(contract.instrumented, util.filePath);
42-
await contract.instance.a();
37+
await contract.instance.a(contract.gas);
4338
const mapping = coverage.generate(contract.data, util.pathPrefix);
4439

4540
assert.deepEqual(mapping[util.filePath].l, {
@@ -56,9 +51,9 @@ describe('function declarations', () => {
5651
});
5752

5853
it('should cover a modifier used on a function', async function() {
59-
const contract = await util.bootstrapCoverage('function/modifier', api);
54+
const contract = await util.bootstrapCoverage('function/modifier', api, this.provider);
6055
coverage.addContract(contract.instrumented, util.filePath);
61-
await contract.instance.a(0);
56+
await contract.instance.a(0, contract.gas);
6257
const mapping = coverage.generate(contract.data, util.pathPrefix);
6358

6459
assert.deepEqual(mapping[util.filePath].l, {

0 commit comments

Comments
 (0)