Skip to content

Commit 6f301d1

Browse files
committed
further test tweaks
1 parent 0f19f94 commit 6f301d1

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

test/helpers/ganache.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ async function revertSnapshot(web3, snapshotId) {
1313
}
1414

1515
async function advanceBlock(web3) {
16+
// let block = await web3.eth.getBlock("latest");
17+
// console.log("evm mine before:", block.number);
18+
1619
await web3.currentProvider.send("evm_mine");
20+
21+
// block = await web3.eth.getBlock("latest");
22+
// console.log("evm mine after:", block.number);
1723
}

test/sendTransaction.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ const baseHelpers = require("./helpers/base.js");
55
const ganache = require("./helpers/ganache.js");
66

77
const providers = baseHelpers.providers;
8+
let accounts;
89

910
providers.forEach(web3 => {
1011
describe("sendTransaction - " + web3.currentProvider.constructor.name, () => {
12+
before(async () => {
13+
accounts = await web3.eth.personal.getAccounts();
14+
});
15+
1116
it("should trigger tx events after send", async () => {
1217
// Works with beta36 HttpProvider but failing with WebsocketProvider(no confirmations received).
1318
// Failing both with beta 51 (sendTransaction never resolves. confirmations are still not received with WebsocketProvider)
1419
return new Promise(async resolve => {
15-
const accounts = await web3.eth.personal.getAccounts();
16-
1720
const transactionHashSpy = sinon.spy();
1821
const confirmationSpy = sinon.spy();
1922
const receiptSpy = sinon.spy();
@@ -23,15 +26,22 @@ providers.forEach(web3 => {
2326
// 2. confirmations event triggered TRANSACTION_CONFIRMATION_BLOCKS times
2427
// 3. tx resolved before confirmation event triggered on the TRANSACTION_CONFIRMATION_BLOCKS time
2528
let resolved = false;
26-
const receipt = await web3.eth
29+
30+
const tx = web3.eth
2731
.sendTransaction({ to: accounts[1], from: accounts[0], value: web3.utils.toWei("0.1", "ether") })
28-
.on("transactionHash", transactionHashSpy)
29-
.on("receipt", receiptSpy)
32+
.on("transactionHash", txHash => {
33+
console.log("transactionHash:", txHash);
34+
transactionHashSpy(txHash);
35+
})
36+
.on("receipt", receipt => {
37+
console.log("receipt received");
38+
receiptSpy(receipt);
39+
})
3040
.on("confirmation", (confirmationNumber, receipt) => {
3141
ganache.advanceBlock(web3); // it seems to be blocked by sendTransaction in beta52
3242
ganache.advanceBlock(web3);
3343
ganache.advanceBlock(web3);
34-
console.log("confirmation", confirmationNumber);
44+
console.log("confirmation", confirmationNumber, web3.currentProvider.constructor.name);
3545
confirmationSpy(confirmationNumber, receipt);
3646
assert(receipt.status);
3747
sinon.assert.calledOnce(transactionHashSpy);
@@ -43,11 +53,52 @@ providers.forEach(web3 => {
4353
}
4454
});
4555

56+
const receipt = await tx;
57+
resolved = true;
58+
4659
assert(receipt.status);
4760
sinon.assert.calledOnce(transactionHashSpy);
4861
sinon.assert.calledOnce(receiptSpy);
49-
resolved = true;
62+
sinon.assert.callCount(confirmationSpy, baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS);
5063
});
5164
}).timeout(baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS * 1000 + 2000);
65+
66+
it("should trigger tx events after send - non async", done => {
67+
const transactionHashSpy = sinon.spy();
68+
const confirmationSpy = sinon.spy();
69+
const receiptSpy = sinon.spy();
70+
71+
let resolved = false;
72+
73+
web3.eth
74+
.sendTransaction({ to: accounts[1], from: accounts[0], value: web3.utils.toWei("0.1", "ether") })
75+
.on("transactionHash", txHash => {
76+
console.log("transactionHash:", txHash);
77+
transactionHashSpy(txHash);
78+
})
79+
.on("receipt", receipt => {
80+
console.log("receipt received");
81+
receiptSpy(receipt);
82+
})
83+
.on("confirmation", (confirmationNumber, receipt) => {
84+
console.log("confirmation", confirmationNumber, web3.currentProvider.constructor.name);
85+
confirmationSpy(confirmationNumber, receipt);
86+
assert(receipt.status);
87+
sinon.assert.calledOnce(transactionHashSpy);
88+
// it's not happening with beta52:
89+
// sinon.assert.calledOnce(receiptSpy);
90+
if (confirmationNumber === baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS) {
91+
assert(resolved);
92+
}
93+
})
94+
.then(receipt => {
95+
resolved = true;
96+
assert(receipt.status);
97+
sinon.assert.calledOnce(transactionHashSpy);
98+
sinon.assert.calledOnce(receiptSpy);
99+
sinon.assert.callCount(confirmationSpy, baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS);
100+
done();
101+
});
102+
}).timeout(baseHelpers.TRANSACTION_CONFIRMATION_BLOCKS * 1000 + 2000);
52103
});
53104
});

test/signTransaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ providers.forEach(web3 => {
5858
receiptSpy(receipt);
5959
})
6060
.on("confirmation", (confirmationNumber, receipt) => {
61-
console.log("confirmation no:", confirmationNumber);
61+
console.log("confirmation no:", confirmationNumber, web3.currentProvider.constructor.name);
6262
confirmationSpy(confirmationNumber, receipt);
6363
})
6464
.on("error", error => console.log);

0 commit comments

Comments
 (0)