Skip to content

Commit f548ad1

Browse files
committed
chore(test): use chai throws() & chai-as-promised isRejected()
1 parent 62774c2 commit f548ad1

7 files changed

+72
-65
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
}
101101
},
102102
"devDependencies": {
103+
"@types/chai-as-promised": "^7.1.4",
103104
"@ipld/dag-pb": "^2.1.14",
104105
"@stablelib/sha256": "^1.0.1",
105106
"@stablelib/sha512": "^1.0.1",
@@ -111,6 +112,7 @@
111112
"buffer": "^6.0.3",
112113
"c8": "^7.10.0",
113114
"chai": "^4.3.4",
115+
"chai-as-promised": "^7.1.1",
114116
"cids": "^1.1.9",
115117
"ipjs": "^5.2.0",
116118
"mocha": "^9.1.3",

test/test-block.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import * as codec from 'multiformats/codecs/json'
33
import { sha256 as hasher } from 'multiformats/hashes/sha2'
44
import * as main from 'multiformats/block'
55
import { CID, bytes } from 'multiformats'
6-
import { assert } from 'chai'
7-
import { testThrowAsync, testThrowSync } from './fixtures/test-throw.js'
6+
import chai from 'chai'
7+
import chaiAsPromised from 'chai-as-promised'
8+
9+
chai.use(chaiAsPromised)
10+
const { assert } = chai
811

912
const fixture = { hello: 'world' }
1013
const link = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
@@ -99,7 +102,7 @@ describe('block', () => {
99102

100103
describe('errors', () => {
101104
it('constructor missing args', () => {
102-
testThrowSync(
105+
assert.throws(
103106
// @ts-expect-error - missing properties
104107
() => new main.Block({}),
105108
'Missing required argument'
@@ -108,36 +111,36 @@ describe('block', () => {
108111

109112
it('encode', async () => {
110113
// @ts-expect-error
111-
await testThrowAsync(() => main.encode({}), 'Missing required argument "value"')
114+
await assert.isRejected(main.encode({}), 'Missing required argument "value"')
112115
// @ts-expect-error
113-
await testThrowAsync(() => main.encode({ value: true }), 'Missing required argument: codec or hasher')
116+
await assert.isRejected(main.encode({ value: true }), 'Missing required argument: codec or hasher')
114117
})
115118

116119
it('decode', async () => {
117120
// @ts-expect-error
118-
await testThrowAsync(() => main.decode({}), 'Missing required argument "bytes"')
121+
await assert.isRejected(main.decode({}), 'Missing required argument "bytes"')
119122
// @ts-expect-error
120-
await testThrowAsync(() => main.decode({ bytes: true }), 'Missing required argument: codec or hasher')
123+
await assert.isRejected(main.decode({ bytes: true }), 'Missing required argument: codec or hasher')
121124
})
122125

123126
it('createUnsafe', async () => {
124127
// @ts-expect-error
125-
await testThrowAsync(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
128+
assert.throws(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
126129
})
127130

128131
it('create', async () => {
129132
// @ts-expect-error
130-
await testThrowAsync(() => main.create({}), 'Missing required argument "bytes"')
133+
await assert.isRejected(main.create({}), 'Missing required argument "bytes"')
131134
// @ts-expect-error
132-
await testThrowAsync(() => main.create({ bytes: true }), 'Missing required argument "hasher"')
135+
await assert.isRejected(main.create({ bytes: true }), 'Missing required argument "hasher"')
133136
const block = await main.encode({ value: fixture, codec, hasher })
134137
const block2 = await main.encode({ value: { ...fixture, test: 'blah' }, codec, hasher })
135-
await testThrowAsync(() => main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes')
138+
await assert.isRejected(main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes')
136139
})
137140

138141
it('get', async () => {
139142
const block = await main.encode({ value: fixture, codec, hasher })
140-
await testThrowAsync(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]')
143+
assert.throws(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]')
141144
})
142145
})
143146
})

test/test-cid.js

+22-32
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
/* globals describe, it */
22

33
import OLDCID from 'cids'
4-
import { assert } from 'chai'
54
import { fromHex, toHex, equals } from '../src/bytes.js'
65
import { varint, CID } from 'multiformats'
76
import { base58btc } from 'multiformats/bases/base58'
87
import { base32 } from 'multiformats/bases/base32'
98
import { base64 } from 'multiformats/bases/base64'
109
import { sha256, sha512 } from 'multiformats/hashes/sha2'
1110
import invalidMultihash from './fixtures/invalid-multihash.js'
12-
import { testThrowSync as testThrow } from './fixtures/test-throw.js'
11+
import chai from 'chai'
12+
import chaiAsPromised from 'chai-as-promised'
1313

14-
const textEncoder = new TextEncoder()
14+
chai.use(chaiAsPromised)
15+
const { assert } = chai
1516

16-
/**
17-
* @param {Function} fn
18-
*/
19-
const testThrowAny = async fn => {
20-
try {
21-
await fn()
22-
} catch (e) {
23-
return
24-
}
25-
/* c8 ignore next */
26-
throw new Error('Test failed to throw')
27-
}
17+
const textEncoder = new TextEncoder()
2818

2919
describe('CID', () => {
3020
describe('v0', () => {
@@ -64,28 +54,28 @@ describe('CID', () => {
6454

6555
it('throws on invalid BS58Str multihash ', async () => {
6656
const msg = 'Non-base58btc character'
67-
await testThrow(() => CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg)
57+
assert.throws(() => CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg)
6858
})
6959

7060
it('throws on trying to create a CIDv0 with a codec other than dag-pb', async () => {
7161
const hash = await sha256.digest(textEncoder.encode('abc'))
7262
const msg = 'Version 0 CID must use dag-pb (code: 112) block encoding'
73-
await testThrow(() => CID.create(0, 113, hash), msg)
63+
assert.throws(() => CID.create(0, 113, hash), msg)
7464
})
7565

7666
// This was failing for quite some time, test just missed await so it went
7767
// unnoticed. Not sure we still care about checking fourth argument.
7868
// it('throws on trying to pass specific base encoding [deprecated]', async () => {
7969
// const hash = await sha256.digest(textEncoder.encode('abc'))
8070
// const msg = 'No longer supported, cannot specify base encoding in instantiation'
81-
// await testThrow(() => CID.create(0, 112, hash, 'base32'), msg)
71+
// assert.throws(() => CID.create(0, 112, hash, 'base32'), msg)
8272
// })
8373

8474
it('throws on trying to base encode CIDv0 in other base than base58btc', async () => {
8575
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
8676
const cid = CID.parse(mhStr)
8777
const msg = 'Cannot string encode V0 in base32 encoding'
88-
await testThrow(() => cid.toString(base32), msg)
78+
assert.throws(() => cid.toString(base32), msg)
8979
})
9080

9181
it('.bytes', async () => {
@@ -272,7 +262,7 @@ describe('CID', () => {
272262

273263
for (const i of parse) {
274264
const name = `CID.parse(${JSON.stringify(i)})`
275-
it(name, async () => await testThrowAny(() => CID.parse(i)))
265+
it(name, async () => assert.throws(() => CID.parse(i)))
276266
}
277267

278268
const decode = [
@@ -282,7 +272,7 @@ describe('CID', () => {
282272

283273
for (const i of decode) {
284274
const name = `CID.decode(textEncoder.encode(${JSON.stringify(i.toString())}))`
285-
it(name, async () => await testThrowAny(() => CID.decode(i)))
275+
it(name, async () => assert.throws(() => CID.decode(i)))
286276
}
287277

288278
const create = [
@@ -296,7 +286,7 @@ describe('CID', () => {
296286
const mh = hash instanceof Uint8Array ? `textEncoder.encode(${form})` : form
297287
const name = `CID.create(${version}, ${code}, ${mh})`
298288
// @ts-expect-error - version issn't always 0|1
299-
it(name, async () => await testThrowAny(() => CID.create(version, code, hash)))
289+
it(name, async () => assert.throws(() => CID.create(version, code, hash)))
300290
}
301291

302292
it('invalid fixtures', async () => {
@@ -333,13 +323,13 @@ describe('CID', () => {
333323
it('should not convert v1 to v0 if not dag-pb codec', async () => {
334324
const hash = await sha256.digest(textEncoder.encode(`TEST${Date.now()}`))
335325
const cid = CID.create(1, 0x71, hash)
336-
await testThrow(() => cid.toV0(), 'Cannot convert a non dag-pb CID to CIDv0')
326+
assert.throws(() => cid.toV0(), 'Cannot convert a non dag-pb CID to CIDv0')
337327
})
338328

339329
it('should not convert v1 to v0 if not sha2-256 multihash', async () => {
340330
const hash = await sha512.digest(textEncoder.encode(`TEST${Date.now()}`))
341331
const cid = CID.create(1, 112, hash)
342-
await testThrow(() => cid.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0')
332+
assert.throws(() => cid.toV0(), 'Cannot convert non sha2-256 multihash CID to CIDv0')
343333
})
344334

345335
it('should return assert.deepStrictEqual instance when converting v1 to v1', async () => {
@@ -523,7 +513,7 @@ describe('CID', () => {
523513
const cid = CID.create(1, 112, hash)
524514
const msg = 'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
525515

526-
await testThrow(() => CID.parse(cid.toString(base64)), msg)
516+
assert.throws(() => CID.parse(cid.toString(base64)), msg)
527517
})
528518

529519
it('parses base64 encoded CIDv1 if base64 is provided', async () => {
@@ -586,39 +576,39 @@ describe('CID', () => {
586576
it('codec', async () => {
587577
const hash = await sha256.digest(textEncoder.encode('abc'))
588578
const cid = CID.create(1, 112, hash)
589-
await testThrow(() => cid.codec, '"codec" property is deprecated, use integer "code" property instead')
579+
assert.throws(() => cid.codec, '"codec" property is deprecated, use integer "code" property instead')
590580
// @ts-expect-error - 'string' is not assignable to parameter of type 'number'
591-
await testThrow(() => CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported')
581+
assert.throws(() => CID.create(1, 'dag-pb', hash), 'String codecs are no longer supported')
592582
})
593583

594584
it('multibaseName', async () => {
595585
const hash = await sha256.digest(textEncoder.encode('abc'))
596586
const cid = CID.create(1, 112, hash)
597-
await testThrow(() => cid.multibaseName, '"multibaseName" property is deprecated')
587+
assert.throws(() => cid.multibaseName, '"multibaseName" property is deprecated')
598588
})
599589

600590
it('prefix', async () => {
601591
const hash = await sha256.digest(textEncoder.encode('abc'))
602592
const cid = CID.create(1, 112, hash)
603-
await testThrow(() => cid.prefix, '"prefix" property is deprecated')
593+
assert.throws(() => cid.prefix, '"prefix" property is deprecated')
604594
})
605595

606596
it('toBaseEncodedString()', async () => {
607597
const hash = await sha256.digest(textEncoder.encode('abc'))
608598
const cid = CID.create(1, 112, hash)
609599
// @ts-expect-error - deprecated
610-
await testThrow(() => cid.toBaseEncodedString(), 'Deprecated, use .toString()')
600+
assert.throws(() => cid.toBaseEncodedString(), 'Deprecated, use .toString()')
611601
})
612602
})
613603

614604
it('invalid CID version', async () => {
615605
const encoded = varint.encodeTo(2, new Uint8Array(32))
616-
await testThrow(() => CID.decode(encoded), 'Invalid CID version 2')
606+
assert.throws(() => CID.decode(encoded), 'Invalid CID version 2')
617607
})
618608

619609
it('buffer', async () => {
620610
const hash = await sha256.digest(textEncoder.encode('abc'))
621611
const cid = CID.create(1, 112, hash)
622-
await testThrow(() => cid.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead')
612+
assert.throws(() => cid.buffer, 'Deprecated .buffer property, use .bytes to get Uint8Array instead')
623613
})
624614
})

test/test-multibase-spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
import { bases } from 'multiformats/basics'
55
import { fromString } from '../src/bytes.js'
6-
import { assert } from 'chai'
7-
import { testThrowSync as testThrow } from './fixtures/test-throw.js'
6+
import chai from 'chai'
7+
import chaiAsPromised from 'chai-as-promised'
8+
9+
chai.use(chaiAsPromised)
10+
const { assert } = chai
811

912
const encoded = [
1013
{
@@ -194,7 +197,7 @@ describe('spec test', () => {
194197
}
195198

196199
console.info('expect', `Non-${base.name} character`)
197-
testThrow(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${base.name} character`)
200+
assert.throws(() => base.decode(base.prefix + '^!@$%!#$%@#y'), `Non-${base.name} character`)
198201
})
199202
}
200203
})

test/test-multibase.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* globals describe, it */
22
import * as bytes from '../src/bytes.js'
3-
import { assert } from 'chai'
43
import * as b2 from 'multiformats/bases/base2'
54
import * as b8 from 'multiformats/bases/base8'
65
import * as b10 from 'multiformats/bases/base10'
@@ -9,7 +8,11 @@ import * as b32 from 'multiformats/bases/base32'
98
import * as b36 from 'multiformats/bases/base36'
109
import * as b58 from 'multiformats/bases/base58'
1110
import * as b64 from 'multiformats/bases/base64'
12-
import { testThrowAsync as testThrow } from './fixtures/test-throw.js'
11+
import chai from 'chai'
12+
import chaiAsPromised from 'chai-as-promised'
13+
14+
chai.use(chaiAsPromised)
15+
const { assert } = chai
1316

1417
const { base16, base32, base58btc, base64 } = { ...b16, ...b32, ...b58, ...b64 }
1518

@@ -40,25 +43,25 @@ describe('multibase', () => {
4043
it('bad chars', () => {
4144
const str = base.prefix + '#$%^&*&^%$#'
4245
const msg = `Non-${base.name} character`
43-
testThrow(() => base.decode(str), msg)
46+
assert.throws(() => base.decode(str), msg)
4447
})
4548
})
4649
}
4750

4851
it('encode string failure', () => {
4952
const msg = 'Unknown type, must be binary type'
5053
// @ts-expect-error - expects bytes
51-
testThrow(() => base32.encode('asdf'), msg)
54+
assert.throws(() => base32.encode('asdf'), msg)
5255
// @ts-expect-error - expects bytes
53-
testThrow(() => base32.encoder.encode('asdf'), msg)
56+
assert.throws(() => base32.encoder.encode('asdf'), msg)
5457
})
5558

5659
it('decode int failure', () => {
5760
const msg = 'Can only multibase decode strings'
5861
// @ts-expect-error - 'number' is not assignable to parameter of type 'string'
59-
testThrow(() => base32.decode(1), msg)
62+
assert.throws(() => base32.decode(1), msg)
6063
// @ts-expect-error - 'number' is not assignable to parameter of type 'string'
61-
testThrow(() => base32.decoder.decode(1), msg)
64+
assert.throws(() => base32.decoder.decode(1), msg)
6265
})
6366

6467
const buff = bytes.fromString('test')
@@ -124,7 +127,7 @@ describe('multibase', () => {
124127
it('multibase mismatch', () => {
125128
const b64 = base64.encode(bytes.fromString('test'))
126129
const msg = `Unable to decode multibase string "${b64}", base32 decoder only supports inputs prefixed with ${base32.prefix}`
127-
testThrow(() => base32.decode(b64), msg)
130+
assert.throws(() => base32.decode(b64), msg)
128131
})
129132

130133
it('decoder composition', () => {
@@ -138,13 +141,13 @@ describe('multibase', () => {
138141

139142
const b64 = base64.encode(bytes.fromString('test'))
140143
const msg = `Unable to decode multibase string "${b64}", only inputs prefixed with ${base32.prefix},${base58btc.prefix} are supported`
141-
testThrow(() => base.decode(b64), msg)
144+
assert.throws(() => base.decode(b64), msg)
142145

143146
const baseExt = base.or(base64)
144147
assert.deepStrictEqual(baseExt.decode(b64), bytes.fromString('test'))
145148

146149
// original composition stays intact
147-
testThrow(() => base.decode(b64), msg)
150+
assert.throws(() => base.decode(b64), msg)
148151

149152
// non-composed combined with composed
150153
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder))
@@ -158,7 +161,7 @@ describe('multibase', () => {
158161
it('truncated data', () => {
159162
const b64 = base64.encode(Uint8Array.from([245, 250]))
160163

161-
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data')
164+
assert.throws(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data')
162165
})
163166

164167
it('infers prefix and name corretly', () => {

test/test-multicodec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/* globals describe, it */
22
import * as bytes from '../src/bytes.js'
3-
import { assert } from 'chai'
43
import * as raw from 'multiformats/codecs/raw'
54
import * as json from 'multiformats/codecs/json'
6-
import { testThrowAsync } from './fixtures/test-throw.js'
5+
import chai from 'chai'
6+
import chaiAsPromised from 'chai-as-promised'
7+
8+
chai.use(chaiAsPromised)
9+
const { assert } = chai
710

811
describe('multicodec', () => {
912
it('encode/decode raw', () => {
@@ -20,6 +23,6 @@ describe('multicodec', () => {
2023

2124
it('raw cannot encode string', async () => {
2225
// @ts-expect-error - 'string' is not assignable to parameter of type 'Uint8Array'
23-
await testThrowAsync(() => raw.encode('asdf'), 'Unknown type, must be binary type')
26+
assert.throws(() => raw.encode('asdf'), 'Unknown type, must be binary type')
2427
})
2528
})

0 commit comments

Comments
 (0)