Skip to content

Commit 4f4d00a

Browse files
Gozalarvagg
authored andcommitted
fix: enable ts on tests
1 parent d19282e commit 4f4d00a

12 files changed

+270
-86
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
"@ipld/dag-pb": "^2.1.14",
104104
"@stablelib/sha256": "^1.0.1",
105105
"@stablelib/sha512": "^1.0.1",
106+
"@types/chai": "^4.3.0",
107+
"@types/mocha": "^9.0.0",
106108
"@types/node": "^17.0.0",
107109
"@typescript-eslint/eslint-plugin": "^5.6.0",
108110
"@typescript-eslint/parser": "^5.6.0",
@@ -114,7 +116,7 @@
114116
"mocha": "^9.1.3",
115117
"polendina": "^2.0.0",
116118
"standard": "^16.0.4",
117-
"typescript": "^4.5.2"
119+
"typescript": "^4.5.4"
118120
},
119121
"standard": {
120122
"ignore": [

src/bases/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ const encode = (data, alphabet, bitsPerChar) => {
352352

353353
/**
354354
* RFC4648 Factory
355-
*
355+
*
356356
* @template {string} Base
357357
* @template {string} Prefix
358358
* @param {Object} options

test/fixtures/test-throw.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
2-
export default function testThrow (fn, message) {
1+
/**
2+
* @param {Function} fn
3+
* @param {string} message
4+
*/
5+
export const testThrowSync = (fn, message) => {
36
try {
47
fn()
58
} catch (e) {
6-
if (e.message !== message) throw e
9+
if (/** @type {Error} */(e).message !== message) throw e
10+
return
11+
}
12+
/* c8 ignore next */
13+
throw new Error('Test failed to throw')
14+
}
15+
16+
/**
17+
* @param {Function} fn
18+
* @param {string} message
19+
*/
20+
export const testThrowAsync = async (fn, message) => {
21+
try {
22+
await fn()
23+
} catch (e) {
24+
if (/** @type {Error} */(e).message !== message) throw e
725
return
826
}
927
/* c8 ignore next */

test/test-block.js

+49-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { sha256 as hasher } from 'multiformats/hashes/sha2'
44
import * as main from 'multiformats/block'
55
import { CID, bytes } from 'multiformats'
66
import { assert } from 'chai'
7+
import { testThrowAsync, testThrowSync } from './fixtures/test-throw.js'
78

89
const fixture = { hello: 'world' }
910
const link = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
@@ -27,7 +28,15 @@ describe('block', () => {
2728
})
2829

2930
describe('reader', () => {
30-
const value = { link, nope: 'skip', arr: [link], obj: { arr: [{ obj: {} }] }, bytes: Uint8Array.from('1234') }
31+
const value = {
32+
link,
33+
nope: 'skip',
34+
arr: [link],
35+
obj: { arr: [{ obj: {} }] },
36+
// @ts-expect-error - 'string' is not assignable to parameter of type 'ArrayLike<number>'
37+
bytes: Uint8Array.from('1234')
38+
}
39+
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
3140
const block = main.createUnsafe({ value, codec, hasher, cid: true, bytes: true })
3241

3342
it('links', () => {
@@ -50,11 +59,20 @@ describe('block', () => {
5059
assert.deepStrictEqual(ret.remaining, 'test')
5160
assert.deepStrictEqual(ret.value.toString(), link.toString())
5261
ret = block.get('nope')
62+
// @ts-expect-error - 'string' is not expected
5363
assert.deepStrictEqual(ret, { value: 'skip' })
5464
})
5565

5666
it('null links/tree', () => {
57-
const block = main.createUnsafe({ value: null, codec, hasher, bytes: true, cid: true })
67+
const block = main.createUnsafe({
68+
value: null,
69+
codec,
70+
hasher,
71+
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>'
72+
bytes: true,
73+
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
74+
cid: true
75+
})
5876
// eslint-disable-next-line
5977
for (const x of block.tree()) {
6078
throw new Error(`tree should have nothing, got "${x}"`)
@@ -68,58 +86,58 @@ describe('block', () => {
6886

6987
it('kitchen sink', () => {
7088
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } }
71-
const block = main.createUnsafe({ value: sink, codec, bytes: true, cid: true })
89+
const block = main.createUnsafe({
90+
value: sink,
91+
codec,
92+
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>'
93+
bytes: true,
94+
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
95+
cid: true
96+
})
7297
assert.deepStrictEqual(sink, block.value)
7398
})
7499

75100
describe('errors', () => {
76101
it('constructor missing args', () => {
77-
let threw = true
78-
try {
79-
threw = new main.Block({})
80-
threw = false
81-
} catch (e) {
82-
if (e.message !== 'Missing required argument') throw e
83-
}
84-
assert.deepStrictEqual(threw, true)
102+
testThrowSync(
103+
// @ts-expect-error - missing properties
104+
() => new main.Block({}),
105+
'Missing required argument'
106+
)
85107
})
86108

87-
const errTest = async (method, arg, message) => {
88-
let threw = true
89-
try {
90-
await method(arg)
91-
threw = false
92-
} catch (e) {
93-
if (e.message !== message) throw e
94-
}
95-
assert.deepStrictEqual(threw, true)
96-
}
97-
98109
it('encode', async () => {
99-
await errTest(main.encode, {}, 'Missing required argument "value"')
100-
await errTest(main.encode, { value: true }, 'Missing required argument: codec or hasher')
110+
// @ts-expect-error
111+
await testThrowAsync(() => main.encode({}), 'Missing required argument "value"')
112+
// @ts-expect-error
113+
await testThrowAsync(() => main.encode({ value: true }), 'Missing required argument: codec or hasher')
101114
})
102115

103116
it('decode', async () => {
104-
await errTest(main.decode, {}, 'Missing required argument "bytes"')
105-
await errTest(main.decode, { bytes: true }, 'Missing required argument: codec or hasher')
117+
// @ts-expect-error
118+
await testThrowAsync(() => main.decode({}), 'Missing required argument "bytes"')
119+
// @ts-expect-error
120+
await testThrowAsync(() => main.decode({ bytes: true }), 'Missing required argument: codec or hasher')
106121
})
107122

108123
it('createUnsafe', async () => {
109-
await errTest(main.createUnsafe, {}, 'Missing required argument, must either provide "value" or "codec"')
124+
// @ts-expect-error
125+
await testThrowAsync(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
110126
})
111127

112128
it('create', async () => {
113-
await errTest(main.create, {}, 'Missing required argument "bytes"')
114-
await errTest(main.create, { bytes: true }, 'Missing required argument "hasher"')
129+
// @ts-expect-error
130+
await testThrowAsync(() => main.create({}), 'Missing required argument "bytes"')
131+
// @ts-expect-error
132+
await testThrowAsync(() => main.create({ bytes: true }), 'Missing required argument "hasher"')
115133
const block = await main.encode({ value: fixture, codec, hasher })
116134
const block2 = await main.encode({ value: { ...fixture, test: 'blah' }, codec, hasher })
117-
await errTest(main.create, { bytes: block.bytes, cid: block2.cid, codec, hasher }, 'CID hash does not match bytes')
135+
await testThrowAsync(() => main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes')
118136
})
119137

120138
it('get', async () => {
121139
const block = await main.encode({ value: fixture, codec, hasher })
122-
await errTest(path => block.get(path), '/asd/fs/dfasd/f', 'Object has no property at ["asd"]')
140+
await testThrowAsync(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]')
123141
})
124142
})
125143
})

test/test-bytes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { assert } from 'chai'
44

55
describe('bytes', () => {
66
it('isBinary', () => {
7-
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer()), true)
8-
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer())), true)
7+
assert.deepStrictEqual(bytes.isBinary(new ArrayBuffer(0)), true)
8+
assert.deepStrictEqual(bytes.isBinary(new DataView(new ArrayBuffer(0))), true)
99
})
1010

1111
it('coerce', () => {

0 commit comments

Comments
 (0)