Skip to content

Commit 033e11e

Browse files
Seulgi Kimmergify[bot]
authored andcommitted
Use chaiAsPromised instead of try/catch
1 parent 660a253 commit 033e11e

File tree

4 files changed

+67
-131
lines changed

4 files changed

+67
-131
lines changed

test/src/e2e.dynval/1/dv.changeParams.test.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,9 @@ describe("Change commonParams that doesn't affects validator set", function() {
267267
seq: await checkingNode.sdk.rpc.chain.getSeq(faucetAddress),
268268
fee: 10
269269
});
270-
try {
271-
await checkingNode.sdk.rpc.chain.sendSignedTransaction(tx);
272-
expect.fail("Low fee should not be included");
273-
} catch (err) {
274-
expect(err.message).contains("Too Low Fee");
275-
}
270+
await expect(
271+
checkingNode.sdk.rpc.chain.sendSignedTransaction(tx)
272+
).rejectedWith(/Too Low Fee/);
276273
});
277274
});
278275

@@ -310,20 +307,16 @@ describe("Change commonParams that doesn't affects validator set", function() {
310307
await checkingNode.waitForTx(normalHash);
311308

312309
const largeNomination = nominationWithMetadata(257);
313-
try {
314-
await checkingNode.sdk.rpc.chain.sendSignedTransaction(
310+
311+
await expect(
312+
checkingNode.sdk.rpc.chain.sendSignedTransaction(
315313
largeNomination.sign({
316314
secret: alice.privateKey,
317315
seq: seq + 1,
318316
fee: 10
319317
})
320-
);
321-
expect.fail(
322-
"Transaction with large metadata should not be included"
323-
);
324-
} catch (err) {
325-
expect(err.message).include("Too long");
326-
}
318+
)
319+
).rejectedWith(/Too long/);
327320
});
328321

329322
it("Should apply smaller metadata limit after decrement", async function() {
@@ -351,20 +344,15 @@ describe("Change commonParams that doesn't affects validator set", function() {
351344
await checkingNode.waitForTx(normalHash);
352345

353346
const largeNomination = nominationWithMetadata(127);
354-
try {
355-
await checkingNode.sdk.rpc.chain.sendSignedTransaction(
347+
await expect(
348+
checkingNode.sdk.rpc.chain.sendSignedTransaction(
356349
largeNomination.sign({
357350
secret: alice.privateKey,
358351
seq: seq + 1,
359352
fee: 10
360353
})
361-
);
362-
expect.fail(
363-
"Transaction with large metadata should not be included"
364-
);
365-
} catch (err) {
366-
expect(err.message).include("Too long");
367-
}
354+
)
355+
).rejectedWith(/Too long/);
368356
});
369357
});
370358

test/src/e2e.dynval/2/jail.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,9 @@ describe("Jail state transition test", function() {
157157
fee: 10
158158
})
159159
);
160-
try {
161-
await node.waitForTx(hash);
162-
expect.fail("Self nomination should not be accepted");
163-
} catch (e) {
164-
expect(e.message).to.contain("Account is still in custody");
165-
}
160+
await expect(node.waitForTx(hash)).rejectedWith(
161+
"Account is still in custody"
162+
);
166163
await termWaiter.waitNodeUntilTerm(node, { target: 3, termPeriods: 1 });
167164
expect(await isPrisoner(alice)).to.be.true;
168165
});

test/src/e2e.long/accountUnlock.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
import * as chai from "chai";
1718
import { expect } from "chai";
19+
import * as chaiAsPromised from "chai-as-promised";
1820
import "mocha";
1921
import { ERROR } from "../helper/error";
2022
import { wait } from "../helper/promise";
2123
import { makeRandomH256, makeRandomPassphrase } from "../helper/random";
2224
import CodeChain from "../helper/spawn";
2325

26+
chai.use(chaiAsPromised);
27+
2428
describe("account unlock", function() {
2529
let node: CodeChain;
2630
const unlockTestSize = 15;

test/src/e2e/changeParams.test.ts

Lines changed: 48 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
import * as chai from "chai";
1718
import { expect } from "chai";
19+
import * as chaiAsPromised from "chai-as-promised";
1820
import { H256, PlatformAddress, U64 } from "codechain-primitives/lib";
1921
import { blake256 } from "codechain-sdk/lib/utils";
2022
import "mocha";
@@ -33,6 +35,8 @@ import CodeChain from "../helper/spawn";
3335

3436
const RLP = require("rlp");
3537

38+
chai.use(chaiAsPromised);
39+
3640
describe("ChangeParams", function() {
3741
const chain = `${__dirname}/../scheme/solo-block-reward-50.json`;
3842
let node: CodeChain;
@@ -103,11 +107,7 @@ describe("ChangeParams", function() {
103107
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.true;
104108
}
105109

106-
try {
107-
await node.sendPayTx({ fee: 10 });
108-
} catch (err) {
109-
expect(err.message).contains("Too Low Fee");
110-
}
110+
await expect(node.sendPayTx({ fee: 10 })).rejectedWith(/Too Low Fee/);
111111

112112
const params = await node.sdk.rpc.sendRpcRequest(
113113
"chain_getCommonParams",
@@ -161,12 +161,9 @@ describe("ChangeParams", function() {
161161
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
162162
fee: 10
163163
});
164-
try {
165-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
166-
expect.fail("The transaction must fail");
167-
} catch (err) {
168-
expect(err.message).contains("network id");
169-
}
164+
await expect(node.sdk.rpc.chain.sendSignedTransaction(tx)).rejectedWith(
165+
/network id/
166+
);
170167
});
171168

172169
it("should keep default common params value", async function() {
@@ -240,11 +237,7 @@ describe("ChangeParams", function() {
240237
);
241238
}
242239

243-
try {
244-
await node.sendPayTx({ fee: 10 });
245-
} catch (err) {
246-
expect(err.message).contains("Too Low Fee");
247-
}
240+
await expect(node.sendPayTx({ fee: 10 })).rejectedWith(/Too Low Fee/);
248241
});
249242

250243
it("the parameter changed twice in the same block", async function() {
@@ -366,11 +359,7 @@ describe("ChangeParams", function() {
366359
const pay = await node.sendPayTx({ fee: 5 });
367360
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
368361
.true;
369-
try {
370-
await node.sendPayTx({ fee: 4 });
371-
} catch (err) {
372-
expect(err.message).contains("Too Low Fee");
373-
}
362+
await expect(node.sendPayTx({ fee: 4 })).rejectedWith(/Too Low Fee/);
374363
});
375364

376365
it("cannot reuse the same signature", async function() {
@@ -492,11 +481,7 @@ describe("ChangeParams", function() {
492481
const pay = await node.sendPayTx({ fee: 5 });
493482
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
494483
.true;
495-
try {
496-
await node.sendPayTx({ fee: 4 });
497-
} catch (err) {
498-
expect(err.message).contains("Too Low Fee");
499-
}
484+
await expect(node.sendPayTx({ fee: 4 })).rejectedWith(/Too Low Fee/);
500485
});
501486

502487
it("cannot change params with insufficient stakes", async function() {
@@ -700,11 +685,7 @@ describe("ChangeParams", function() {
700685
expect(await node.sdk.rpc.chain.getTransaction(hash)).not.be.null;
701686
}
702687

703-
try {
704-
await node.sendPayTx({ fee: 10 });
705-
} catch (err) {
706-
expect(err.message).contains("Too Low Fee");
707-
}
688+
await expect(node.sendPayTx({ fee: 10 })).rejectedWith(/Too Low Fee/);
708689
});
709690

710691
describe("with stake parameters", async function() {
@@ -764,11 +745,9 @@ describe("ChangeParams", function() {
764745
.true;
765746
}
766747

767-
try {
768-
await node.sendPayTx({ fee: 10 });
769-
} catch (err) {
770-
expect(err.message).contains("Too Low Fee");
771-
}
748+
await expect(node.sendPayTx({ fee: 10 })).rejectedWith(
749+
/Too Low Fee/
750+
);
772751

773752
const params = await node.sdk.rpc.sendRpcRequest(
774753
"chain_getCommonParams",
@@ -835,12 +814,9 @@ describe("ChangeParams", function() {
835814
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
836815
fee: 10
837816
});
838-
try {
839-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
840-
expect.fail("The transaction must fail");
841-
} catch (err) {
842-
expect(err.message).contains("nomination expiration");
843-
}
817+
await expect(
818+
node.sdk.rpc.chain.sendSignedTransaction(tx)
819+
).rejectedWith(/nomination expiration/);
844820
});
845821

846822
it("custody period cannot be zero", async function() {
@@ -901,12 +877,9 @@ describe("ChangeParams", function() {
901877
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
902878
fee: 10
903879
});
904-
try {
905-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
906-
expect.fail("The transaction must fail");
907-
} catch (err) {
908-
expect(err.message).contains("custody period");
909-
}
880+
await expect(
881+
node.sdk.rpc.chain.sendSignedTransaction(tx)
882+
).rejectedWith(/custody period/);
910883
});
911884

912885
it("release period cannot be zero", async function() {
@@ -967,12 +940,9 @@ describe("ChangeParams", function() {
967940
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
968941
fee: 10
969942
});
970-
try {
971-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
972-
expect.fail("The transaction must fail");
973-
} catch (err) {
974-
expect(err.message).contains("release period");
975-
}
943+
await expect(
944+
node.sdk.rpc.chain.sendSignedTransaction(tx)
945+
).rejectedWith(/release period/);
976946
});
977947

978948
it("The maximum number of candidates cannot be equal to the minimum number of candidates", async function() {
@@ -1100,11 +1070,9 @@ describe("ChangeParams", function() {
11001070
.true;
11011071
}
11021072

1103-
try {
1104-
await node.sendPayTx({ fee: 10 });
1105-
} catch (err) {
1106-
expect(err.message).contains("Too Low Fee");
1107-
}
1073+
await expect(node.sendPayTx({ fee: 10 })).rejectedWith(
1074+
/Too Low Fee/
1075+
);
11081076

11091077
const params = await node.sdk.rpc.sendRpcRequest(
11101078
"chain_getCommonParams",
@@ -1171,12 +1139,9 @@ describe("ChangeParams", function() {
11711139
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
11721140
fee: 10
11731141
});
1174-
try {
1175-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1176-
expect.fail("The transaction must fail");
1177-
} catch (err) {
1178-
expect(err.message).contains("nomination expiration");
1179-
}
1142+
await expect(
1143+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1144+
).rejectedWith(/nomination expiration/);
11801145
});
11811146

11821147
it("custody period cannot be zero", async function() {
@@ -1237,12 +1202,9 @@ describe("ChangeParams", function() {
12371202
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
12381203
fee: 10
12391204
});
1240-
try {
1241-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1242-
expect.fail("The transaction must fail");
1243-
} catch (err) {
1244-
expect(err.message).contains("custody period");
1245-
}
1205+
await expect(
1206+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1207+
).rejectedWith(/custody period/);
12461208
});
12471209

12481210
it("release period cannot be zero", async function() {
@@ -1303,12 +1265,9 @@ describe("ChangeParams", function() {
13031265
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
13041266
fee: 10
13051267
});
1306-
try {
1307-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1308-
expect.fail("The transaction must fail");
1309-
} catch (err) {
1310-
expect(err.message).contains("release period");
1311-
}
1268+
await expect(
1269+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1270+
).rejectedWith(/release period/);
13121271
});
13131272

13141273
it("min deposit cannot be zero", async function() {
@@ -1369,12 +1328,9 @@ describe("ChangeParams", function() {
13691328
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
13701329
fee: 10
13711330
});
1372-
try {
1373-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1374-
expect.fail("The transaction must fail");
1375-
} catch (err) {
1376-
expect(err.message).contains("minimum deposit");
1377-
}
1331+
await expect(
1332+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1333+
).rejectedWith(/minimum deposit/);
13781334
});
13791335

13801336
it("delegation threshold cannot be zero", async function() {
@@ -1435,12 +1391,9 @@ describe("ChangeParams", function() {
14351391
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
14361392
fee: 10
14371393
});
1438-
try {
1439-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1440-
expect.fail("The transaction must fail");
1441-
} catch (err) {
1442-
expect(err.message).contains("delegation threshold");
1443-
}
1394+
await expect(
1395+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1396+
).rejectedWith(/delegation threshold/);
14441397
});
14451398

14461399
it("min number of validators cannot be zero", async function() {
@@ -1501,12 +1454,9 @@ describe("ChangeParams", function() {
15011454
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
15021455
fee: 10
15031456
});
1504-
try {
1505-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1506-
expect.fail("The transaction must fail");
1507-
} catch (err) {
1508-
expect(err.message).contains("minimum number of validators");
1509-
}
1457+
await expect(
1458+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1459+
).rejectedWith(/minimum number of validators/);
15101460
});
15111461

15121462
it("max number of validators cannot be zero", async function() {
@@ -1567,12 +1517,9 @@ describe("ChangeParams", function() {
15671517
seq: await node.sdk.rpc.chain.getSeq(faucetAddress),
15681518
fee: 10
15691519
});
1570-
try {
1571-
await node.sdk.rpc.chain.sendSignedTransaction(tx);
1572-
expect.fail("The transaction must fail");
1573-
} catch (err) {
1574-
expect(err.message).contains("maximum number of validators");
1575-
}
1520+
await expect(
1521+
node.sdk.rpc.chain.sendSignedTransaction(tx)
1522+
).rejectedWith(/maximum number of validators/);
15761523
});
15771524

15781525
it("The maximum number of candidates cannot be equal to the minimum number of candidates", async function() {

0 commit comments

Comments
 (0)