Skip to content

Commit 127a626

Browse files
authored
Encoder tests now look for specific errors (#5710)
This improves the reliability of these tests by ensuring they are receiving the errors they are actually looking for. Without these checks, the code could begin throwing an unrelated error and the unit tests would not catch it.
1 parent cb02271 commit 127a626

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

ironfish/src/wallet/exporter/encoders/base64json.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ describe('Base64JsonEncoder', () => {
184184
it('throws an error when decoding strings without the prefix', () => {
185185
const encoded = 'not base64'
186186

187-
expect(() => encoder.decode(encoded)).toThrow()
187+
expect(() => encoder.decode(encoded)).toThrow('Invalid prefix for base64 encoded account')
188188
})
189189

190190
it('throws an error when decoding non-base64 strings', () => {
191191
const encoded = 'ifaccountnot base64'
192192

193-
expect(() => encoder.decode(encoded)).toThrow()
193+
expect(() => encoder.decode(encoded)).toThrow('Invalid JSON')
194194
})
195195
})

ironfish/src/wallet/exporter/encoders/bech32.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,15 @@ describe('Bech32AccountEncoder', () => {
165165
it('throws an error if it cannot decode the bech32 string', () => {
166166
const encoded = Bech32m.encode('incorrect serialization', BECH32_ACCOUNT_PREFIX)
167167

168-
expect(() => encoder.decode(encoded)).toThrow()
168+
expect(() => encoder.decode(encoded)).toThrow(
169+
'Bufio decoding failed while using bech32 encoder',
170+
)
169171
})
170172

171173
it('throws an error when decoding non-bech32 strings', () => {
172174
const encoded = 'not bech32'
173175

174-
expect(() => encoder.decode(encoded)).toThrow()
176+
expect(() => encoder.decode(encoded)).toThrow('Could not decode account')
175177
})
176178

177179
it('throws an error when decoding if the version is not supported', () => {
@@ -192,6 +194,6 @@ describe('Bech32AccountEncoder', () => {
192194
const encoded = encoder.encode(accountImport)
193195
expect(encoded.startsWith(BECH32_ACCOUNT_PREFIX)).toBe(true)
194196

195-
expect(() => encoder.decode(encoded)).toThrow()
197+
expect(() => encoder.decode(encoded)).toThrow('Encoded account version 0 not supported')
196198
})
197199
})

ironfish/src/wallet/exporter/encoders/bech32json.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Bech32JsonEncoder', () => {
3838
it('throws when bech32 decoded string is not json account', () => {
3939
const invalidJson = 'ironfishaccount1qqqqqqqqc5n9p2'
4040
const encoder = new Bech32JsonEncoder()
41-
expect(() => encoder.decode(invalidJson)).toThrow()
41+
expect(() => encoder.decode(invalidJson)).toThrow('Invalid JSON')
4242
})
4343

4444
it('derives missing viewKeys from the spendingKey', () => {

ironfish/src/wallet/exporter/encoders/json.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('JsonEncoder', () => {
5151
it('throws when json is not a valid account', () => {
5252
const invalidJson = '{}'
5353
const encoder = new JsonEncoder()
54-
expect(() => encoder.decode(invalidJson)).toThrow()
54+
expect(() => encoder.decode(invalidJson)).toThrow('Invalid Schema')
5555
})
5656

5757
it('derives missing viewKeys from the spendingKey', () => {

ironfish/src/wallet/exporter/encoders/mnemonic.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ describe('MnemonicEncoder', () => {
1616
const encoded = encoder.encode(decoded, { language: 'English' })
1717
expect(encoded).toEqual(mnemonic)
1818
})
19+
1920
it('should throw with invalid mnemonic', () => {
2021
const mnemonic = 'invalid mnemonic'
2122
const encoder = new MnemonicEncoder()
22-
expect(() => encoder.decode(mnemonic, { name: 'foo' })).toThrow()
23+
expect(() => encoder.decode(mnemonic, { name: 'foo' })).toThrow('Invalid mnemonic')
2324
})
2425
})
2526
})

ironfish/src/wallet/exporter/encoders/spendingKey.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ describe('SpendingKeyEncoder', () => {
1515
const encoded = encoder.encode(decoded)
1616
expect(encoded).toEqual(spendingKey)
1717
})
18+
1819
it('should throw with invalid spending key', () => {
1920
const invalidSpendingKey = 'foo'
2021
const encoder = new SpendingKeyEncoder()
21-
expect(() => encoder.decode(invalidSpendingKey, { name: 'key' })).toThrow()
22+
expect(() => encoder.decode(invalidSpendingKey, { name: 'key' })).toThrow(
23+
'Invalid spending key',
24+
)
2225
})
2326
})
2427
})

0 commit comments

Comments
 (0)