@@ -53,6 +53,22 @@ describe('multihash', () => {
53
53
assert . deepStrictEqual ( hash2 . bytes , hash . bytes )
54
54
} )
55
55
56
+ if ( typeof navigator === 'undefined' ) {
57
+ it ( 'sync sha-256' , ( ) => {
58
+ const hash = sha256 . digest ( fromString ( 'test' ) )
59
+ if ( hash instanceof Promise ) {
60
+ assert . fail ( 'expected sync result' )
61
+ } else {
62
+ assert . deepStrictEqual ( hash . code , sha256 . code )
63
+ assert . deepStrictEqual ( hash . digest , slSha256 ( fromString ( 'test' ) ) )
64
+
65
+ const hash2 = decodeDigest ( hash . bytes )
66
+ assert . deepStrictEqual ( hash2 . code , sha256 . code )
67
+ assert . deepStrictEqual ( hash2 . bytes , hash . bytes )
68
+ }
69
+ } )
70
+ }
71
+
56
72
it ( 'hash sha2-512' , async ( ) => {
57
73
const hash = await sha512 . digest ( fromString ( 'test' ) )
58
74
assert . deepStrictEqual ( hash . code , sha512 . code )
@@ -63,7 +79,7 @@ describe('multihash', () => {
63
79
assert . deepStrictEqual ( hash2 . bytes , hash . bytes )
64
80
} )
65
81
66
- it ( 'hash identity' , async ( ) => {
82
+ it ( 'hash identity async ' , async ( ) => {
67
83
const hash = await identity . digest ( fromString ( 'test' ) )
68
84
assert . deepStrictEqual ( hash . code , identity . code )
69
85
assert . deepStrictEqual ( identity . code , 0 )
@@ -73,6 +89,17 @@ describe('multihash', () => {
73
89
assert . deepStrictEqual ( hash2 . code , identity . code )
74
90
assert . deepStrictEqual ( hash2 . bytes , hash . bytes )
75
91
} )
92
+
93
+ it ( 'hash identity sync' , ( ) => {
94
+ const hash = identity . digest ( fromString ( 'test' ) )
95
+ assert . deepStrictEqual ( hash . code , identity . code )
96
+ assert . deepStrictEqual ( identity . code , 0 )
97
+ assert . deepStrictEqual ( hash . digest , fromString ( 'test' ) )
98
+
99
+ const hash2 = decodeDigest ( hash . bytes )
100
+ assert . deepStrictEqual ( hash2 . code , identity . code )
101
+ assert . deepStrictEqual ( hash2 . bytes , hash . bytes )
102
+ } )
76
103
} )
77
104
describe ( 'decode' , ( ) => {
78
105
for ( const { encoding, hex, size } of valid ) {
@@ -105,7 +132,11 @@ describe('multihash', () => {
105
132
} )
106
133
107
134
it ( 'throw on hashing non-buffer' , async ( ) => {
108
- // @ts -expect-error - string is incompatible arg
109
- await assert . isRejected ( sha256 . digest ( 'asdf' ) , 'Unknown type, must be binary type' )
135
+ try {
136
+ // @ts -expect-error - string is incompatible arg
137
+ await sha256 . digest ( 'asdf' )
138
+ } catch ( error ) {
139
+ assert . match ( String ( error ) , / U n k n o w n t y p e , m u s t b e b i n a r y t y p e / )
140
+ }
110
141
} )
111
142
} )
0 commit comments