Skip to content

Commit ca243f5

Browse files
authored
Merge pull request #1832 from dynst/then-expect-error
replace .then().catch() in tests
2 parents af85b5e + 153d01a commit ca243f5

File tree

14 files changed

+95
-261
lines changed

14 files changed

+95
-261
lines changed

packages/crypto/src/libsodium.spec.ts

Lines changed: 39 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -127,41 +127,25 @@ describe("Libsodium", () => {
127127
};
128128

129129
// 8 bytes
130-
await Argon2id.execute(password, fromHex("aabbccddeeff0011"), options)
131-
.then(() => {
132-
throw new Error("Argon2id with invalid salt length must not resolve");
133-
})
134-
.catch((e) => {
135-
expect(e).toMatch(/invalid salt length/);
136-
});
130+
await expectAsync(
131+
Argon2id.execute(password, fromHex("aabbccddeeff0011"), options),
132+
).toBeRejectedWithError(/invalid salt length/);
137133
// 15 bytes
138-
await Argon2id.execute(password, fromHex("aabbccddeeff001122334455667788"), options)
139-
.then(() => {
140-
throw new Error("Argon2id with invalid salt length must not resolve");
141-
})
142-
.catch((e) => {
143-
expect(e).toMatch(/invalid salt length/);
144-
});
134+
await expectAsync(
135+
Argon2id.execute(password, fromHex("aabbccddeeff001122334455667788"), options),
136+
).toBeRejectedWithError(/invalid salt length/);
145137
// 17 bytes
146-
await Argon2id.execute(password, fromHex("aabbccddeeff00112233445566778899aa"), options)
147-
.then(() => {
148-
throw new Error("Argon2id with invalid salt length must not resolve");
149-
})
150-
.catch((e) => {
151-
expect(e).toMatch(/invalid salt length/);
152-
});
138+
await expectAsync(
139+
Argon2id.execute(password, fromHex("aabbccddeeff00112233445566778899aa"), options),
140+
).toBeRejectedWithError(/invalid salt length/);
153141
// 32 bytes
154-
await Argon2id.execute(
155-
password,
156-
fromHex("aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899"),
157-
options,
158-
)
159-
.then(() => {
160-
throw new Error("Argon2id with invalid salt length must not resolve");
161-
})
162-
.catch((e) => {
163-
expect(e).toMatch(/invalid salt length/);
164-
});
142+
await expectAsync(
143+
Argon2id.execute(
144+
password,
145+
fromHex("aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899"),
146+
options,
147+
),
148+
).toBeRejectedWithError(/invalid salt length/);
165149
});
166150
});
167151

@@ -233,25 +217,13 @@ describe("Libsodium", () => {
233217
{
234218
// seed too short
235219
const seed = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0");
236-
await Ed25519.makeKeypair(seed)
237-
.then(() => {
238-
throw new Error("promise must not resolve");
239-
})
240-
.catch((error) => {
241-
expect(error.message).toContain("key of length 32 expected");
242-
});
220+
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(/key of length 32 expected/);
243221
}
244222

245223
{
246224
// seed too long
247225
const seed = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9aa");
248-
await Ed25519.makeKeypair(seed)
249-
.then(() => {
250-
throw new Error("promise must not resolve");
251-
})
252-
.catch((error) => {
253-
expect(error.message).toContain("key of length 32 expected");
254-
});
226+
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(/key of length 32 expected/);
255227
}
256228
});
257229

@@ -421,48 +393,32 @@ describe("Libsodium", () => {
421393
{
422394
// empty
423395
const key = fromHex("");
424-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce)
425-
.then(() => {
426-
throw new Error("encryption must not succeed");
427-
})
428-
.catch((error) => {
429-
expect(error).toMatch(/key, got length=0/);
430-
});
396+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
397+
/key, got length=0/,
398+
);
431399
}
432400
{
433401
// 31 bytes
434402
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916");
435-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce)
436-
.then(() => {
437-
throw new Error("encryption must not succeed");
438-
})
439-
.catch((error) => {
440-
expect(error).toMatch(/key, got length=31/);
441-
});
403+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
404+
/key, got length=31/,
405+
);
442406
}
443407
{
444408
// 33 bytes
445409
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8aa");
446-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce)
447-
.then(() => {
448-
throw new Error("encryption must not succeed");
449-
})
450-
.catch((error) => {
451-
expect(error).toMatch(/key, got length=33/);
452-
});
410+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
411+
/key, got length=33/,
412+
);
453413
}
454414
{
455415
// 64 bytes
456416
const key = fromHex(
457417
"1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d81324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8",
458418
);
459-
await Xchacha20poly1305Ietf.encrypt(message, key, nonce)
460-
.then(() => {
461-
throw new Error("encryption must not succeed");
462-
})
463-
.catch((error) => {
464-
expect(error).toMatch(/key, got length=64/);
465-
});
419+
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
420+
/key, got length=64/,
421+
);
466422
}
467423
});
468424

@@ -482,38 +438,23 @@ describe("Libsodium", () => {
482438
{
483439
// corrupted ciphertext
484440
const corruptedCiphertext = ciphertext.map((x, i) => (i === 0 ? x ^ 0x01 : x));
485-
await Xchacha20poly1305Ietf.decrypt(corruptedCiphertext, key, nonce).then(
486-
() => {
487-
throw new Error("promise must not resolve");
488-
},
489-
(error) => {
490-
expect(error.message).toMatch(/invalid tag/i);
491-
},
492-
);
441+
await expectAsync(
442+
Xchacha20poly1305Ietf.decrypt(corruptedCiphertext, key, nonce),
443+
).toBeRejectedWithError(/invalid tag/i);
493444
}
494445
{
495446
// corrupted key
496447
const corruptedKey = key.map((x, i) => (i === 0 ? x ^ 0x01 : x));
497-
await Xchacha20poly1305Ietf.decrypt(ciphertext, corruptedKey, nonce).then(
498-
() => {
499-
throw new Error("promise must not resolve");
500-
},
501-
(error) => {
502-
expect(error.message).toMatch(/invalid tag/i);
503-
},
504-
);
448+
await expectAsync(
449+
Xchacha20poly1305Ietf.decrypt(ciphertext, corruptedKey, nonce),
450+
).toBeRejectedWithError(/invalid tag/i);
505451
}
506452
{
507453
// corrupted nonce
508454
const corruptedNonce = nonce.map((x, i) => (i === 0 ? x ^ 0x01 : x));
509-
await Xchacha20poly1305Ietf.decrypt(ciphertext, key, corruptedNonce).then(
510-
() => {
511-
throw new Error("promise must not resolve");
512-
},
513-
(error) => {
514-
expect(error.message).toMatch(/invalid tag/i);
515-
},
516-
);
455+
await expectAsync(
456+
Xchacha20poly1305Ietf.decrypt(ciphertext, key, corruptedNonce),
457+
).toBeRejectedWithError(/invalid tag/i);
517458
}
518459
});
519460

packages/crypto/src/secp256k1.spec.ts

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,23 @@ describe("Secp256k1", () => {
6868
).toBeTruthy();
6969

7070
// too short and too long
71-
await Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc17"))
72-
.then(() => {
73-
throw new Error("promise must be rejected");
74-
})
75-
.catch((error) => {
76-
expect(error.message).toContain("not a valid secp256k1 private key");
77-
});
78-
await Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc1732aa"))
79-
.then(() => {
80-
throw new Error("promise must be rejected");
81-
})
82-
.catch((error) => {
83-
expect(error.message).toContain("not a valid secp256k1 private key");
84-
});
71+
await expectAsync(
72+
Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc17")),
73+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
74+
await expectAsync(
75+
Secp256k1.makeKeypair(fromHex("e4ade2a5232a7c6f37e7b854a774e25e6047ee7c6d63e8304ae04fa190bc1732aa")),
76+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
8577
// value out of range (too small)
86-
await Secp256k1.makeKeypair(fromHex("0000000000000000000000000000000000000000000000000000000000000000"))
87-
.then(() => {
88-
throw new Error("promise must be rejected");
89-
})
90-
.catch((error) => {
91-
expect(error.message).toContain("not a valid secp256k1 private key");
92-
});
78+
await expectAsync(
79+
Secp256k1.makeKeypair(fromHex("0000000000000000000000000000000000000000000000000000000000000000")),
80+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
9381
// value out of range (>= n)
94-
await Secp256k1.makeKeypair(fromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
95-
.then(() => {
96-
throw new Error("promise must be rejected");
97-
})
98-
.catch((error) => {
99-
expect(error.message).toContain("not a valid secp256k1 private key");
100-
});
101-
await Secp256k1.makeKeypair(fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"))
102-
.then(() => {
103-
throw new Error("promise must be rejected");
104-
})
105-
.catch((error) => {
106-
expect(error.message).toContain("not a valid secp256k1 private key");
107-
});
82+
await expectAsync(
83+
Secp256k1.makeKeypair(fromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")),
84+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
85+
await expectAsync(
86+
Secp256k1.makeKeypair(fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")),
87+
).toBeRejectedWithError("input data is not a valid secp256k1 private key");
10888
});
10989

11090
it("creates signatures", async () => {
@@ -131,26 +111,18 @@ describe("Secp256k1", () => {
131111
const privkey = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9");
132112
const keypair = await Secp256k1.makeKeypair(privkey);
133113
const messageHash = new Uint8Array([]);
134-
await Secp256k1.createSignature(messageHash, keypair.privkey)
135-
.then(() => {
136-
throw new Error("must not resolve");
137-
})
138-
.catch((error) => {
139-
expect(error).toMatch(/message hash must not be empty/i);
140-
});
114+
await expectAsync(Secp256k1.createSignature(messageHash, keypair.privkey)).toBeRejectedWithError(
115+
/message hash must not be empty/i,
116+
);
141117
});
142118

143119
it("throws for message hash longer than 32 bytes in signing", async () => {
144120
const privkey = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9");
145121
const keypair = await Secp256k1.makeKeypair(privkey);
146122
const messageHash = fromHex("11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff11");
147-
await Secp256k1.createSignature(messageHash, keypair.privkey)
148-
.then(() => {
149-
throw new Error("must not resolve");
150-
})
151-
.catch((error) => {
152-
expect(error).toMatch(/message hash length must not exceed 32 bytes/i);
153-
});
123+
await expectAsync(Secp256k1.createSignature(messageHash, keypair.privkey)).toBeRejectedWithError(
124+
/message hash length must not exceed 32 bytes/i,
125+
);
154126
});
155127

156128
it("verifies signatures", async () => {
@@ -200,13 +172,9 @@ describe("Secp256k1", () => {
200172
fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9"),
201173
);
202174
const messageHash = new Uint8Array([]);
203-
await Secp256k1.verifySignature(dummySignature, messageHash, keypair.pubkey)
204-
.then(() => {
205-
throw new Error("must not resolve");
206-
})
207-
.catch((error) => {
208-
expect(error).toMatch(/message hash must not be empty/i);
209-
});
175+
await expectAsync(
176+
Secp256k1.verifySignature(dummySignature, messageHash, keypair.pubkey),
177+
).toBeRejectedWithError(/message hash must not be empty/i);
210178
});
211179

212180
it("throws for message hash longer than 32 bytes in verification", async () => {
@@ -219,13 +187,9 @@ describe("Secp256k1", () => {
219187
fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9"),
220188
);
221189
const messageHash = fromHex("11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff11");
222-
await Secp256k1.verifySignature(dummySignature, messageHash, keypair.privkey)
223-
.then(() => {
224-
throw new Error("must not resolve");
225-
})
226-
.catch((error) => {
227-
expect(error).toMatch(/message hash length must not exceed 32 bytes/i);
228-
});
190+
await expectAsync(
191+
Secp256k1.verifySignature(dummySignature, messageHash, keypair.privkey),
192+
).toBeRejectedWithError(/message hash length must not exceed 32 bytes/i);
229193
});
230194

231195
it("verifies unnormalized pyca/cryptography signatures", async () => {

packages/faucet-client/src/faucetclient.spec.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,15 @@ describe("FaucetClient", () => {
3333

3434
(enabled ? it : xit)("throws for invalid ticker", async () => {
3535
const faucet = new FaucetClient(faucetUrl);
36-
await faucet.credit(defaultAddress, "ETH").then(
37-
() => {
38-
throw new Error("must not resolve");
39-
},
40-
(error) => {
41-
expect(error).toMatch(/token is not available/i);
42-
},
43-
);
36+
await expectAsync(faucet.credit(defaultAddress, "ETH")).toBeRejectedWithError(/token is not available/i);
4437
});
4538

4639
(enabled ? it : xit)("throws for invalid address", async () => {
4740
const faucet = new FaucetClient(faucetUrl);
4841

4942
for (const address of ["be5cc2cc05db2cdb4313c18306a5157291cfdcd1", "1234L"]) {
50-
await faucet.credit(address, primaryToken).then(
51-
() => {
52-
throw new Error("must not resolve");
53-
},
54-
(error) => {
55-
expect(error).toMatch(/address is not in the expected format for this chain/i);
56-
},
43+
await expectAsync(faucet.credit(address, primaryToken)).toBeRejectedWithError(
44+
/address is not in the expected format for this chain/i,
5745
);
5846
}
5947
});

packages/socket/src/socketwrapper.spec.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,7 @@ const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
147147
);
148148
socket.connect();
149149

150-
await socket.connected
151-
.then(() => {
152-
throw new Error("must not resolve");
153-
})
154-
.catch((error) => {
155-
expect(error).toMatch(/connection attempt timed out/i);
156-
});
150+
await expectAsync(socket.connected).toBeRejectedWithError(/connection attempt timed out/i);
157151
});
158152

159153
it("can connect and disconnect", async () => {
@@ -326,7 +320,7 @@ const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
326320
});
327321

328322
it("cannot send on a disconnect socket (it will never come back)", async () => {
329-
let done!: (() => void) & { fail: (e?: any) => void };
323+
let done!: ((p?: void | PromiseLike<void>) => void) & { fail: (e?: any) => void };
330324
const ret = new Promise<void>((resolve, reject) => {
331325
done = resolve as typeof done;
332326
done.fail = reject;
@@ -344,15 +338,7 @@ const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
344338
socket.disconnect();
345339
},
346340
() => {
347-
socket
348-
.send("la li lu")
349-
.then(() => {
350-
done.fail("must not resolve");
351-
})
352-
.catch((error) => {
353-
expect(error).toMatch(/socket was closed/i);
354-
done();
355-
});
341+
done(expectAsync(socket.send("la li lu")).toBeRejectedWithError(/socket was closed/i));
356342
},
357343
);
358344
socket.connect();

0 commit comments

Comments
 (0)