@@ -4,6 +4,7 @@ import { sha256 as hasher } from 'multiformats/hashes/sha2'
4
4
import * as main from 'multiformats/block'
5
5
import { CID , bytes } from 'multiformats'
6
6
import { assert } from 'chai'
7
+ import { testThrowAsync , testThrowSync } from './fixtures/test-throw.js'
7
8
8
9
const fixture = { hello : 'world' }
9
10
const link = CID . parse ( 'bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae' )
@@ -27,7 +28,15 @@ describe('block', () => {
27
28
} )
28
29
29
30
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'
31
40
const block = main . createUnsafe ( { value, codec, hasher, cid : true , bytes : true } )
32
41
33
42
it ( 'links' , ( ) => {
@@ -50,11 +59,20 @@ describe('block', () => {
50
59
assert . deepStrictEqual ( ret . remaining , 'test' )
51
60
assert . deepStrictEqual ( ret . value . toString ( ) , link . toString ( ) )
52
61
ret = block . get ( 'nope' )
62
+ // @ts -expect-error - 'string' is not expected
53
63
assert . deepStrictEqual ( ret , { value : 'skip' } )
54
64
} )
55
65
56
66
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
+ } )
58
76
// eslint-disable-next-line
59
77
for ( const x of block . tree ( ) ) {
60
78
throw new Error ( `tree should have nothing, got "${ x } "` )
@@ -68,58 +86,58 @@ describe('block', () => {
68
86
69
87
it ( 'kitchen sink' , ( ) => {
70
88
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
+ } )
72
97
assert . deepStrictEqual ( sink , block . value )
73
98
} )
74
99
75
100
describe ( 'errors' , ( ) => {
76
101
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
+ )
85
107
} )
86
108
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
-
98
109
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' )
101
114
} )
102
115
103
116
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' )
106
121
} )
107
122
108
123
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"' )
110
126
} )
111
127
112
128
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"' )
115
133
const block = await main . encode ( { value : fixture , codec, hasher } )
116
134
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' )
118
136
} )
119
137
120
138
it ( 'get' , async ( ) => {
121
139
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"]' )
123
141
} )
124
142
} )
125
143
} )
0 commit comments