@@ -51,6 +51,7 @@ jest.mock("@aws-sdk/client-s3", () => ({
51
51
} ) ) ;
52
52
53
53
import { CompleteMultipartUploadCommandOutput , S3 } from "@aws-sdk/client-s3" ;
54
+ import { createHash } from "crypto" ;
54
55
import { Readable } from "stream" ;
55
56
56
57
import { Progress , Upload } from "./index" ;
@@ -76,6 +77,24 @@ describe(Upload.name, () => {
76
77
Body : "this-is-a-sample-payload" ,
77
78
} ;
78
79
80
+ expect . extend ( {
81
+ toHaveSameHashAsBuffer : ( received : Uint8Array , expected : Uint8Array ) => {
82
+ const receivedHash = createHash ( "sha256" ) . update ( received ) . digest ( "hex" ) ;
83
+ const expectHash = createHash ( "sha256" ) . update ( expected ) . digest ( "hex" ) ;
84
+ if ( expectHash === receivedHash ) {
85
+ return {
86
+ message : ( ) => "received buffer has the correct hash" ,
87
+ pass : true ,
88
+ } ;
89
+ } else {
90
+ return {
91
+ message : ( ) => `received buffer hash is incorrect, expect ${ expectHash } , got ${ receivedHash } .` ,
92
+ pass : false ,
93
+ } ;
94
+ }
95
+ } ,
96
+ } ) ;
97
+
79
98
it ( "correctly exposes the event emitter API" , ( ) => {
80
99
const upload = new Upload ( {
81
100
params,
@@ -237,33 +256,30 @@ describe(Upload.name, () => {
237
256
params : actionParams ,
238
257
client : new S3 ( { } ) ,
239
258
} ) ;
240
-
241
259
await upload . done ( ) ;
242
-
243
260
expect ( sendMock ) . toHaveBeenCalledTimes ( 4 ) ;
244
261
// create multipartMock is called correctly.
245
262
expect ( createMultipartMock ) . toHaveBeenCalledTimes ( 1 ) ;
246
263
expect ( createMultipartMock ) . toHaveBeenCalledWith ( {
247
264
...actionParams ,
248
265
Body : undefined ,
249
266
} ) ;
250
-
251
267
// upload parts is called correctly.
252
268
expect ( uploadPartMock ) . toHaveBeenCalledTimes ( 2 ) ;
253
269
expect ( uploadPartMock ) . toHaveBeenNthCalledWith ( 1 , {
254
270
...actionParams ,
255
- Body : firstBuffer ,
271
+ // @ts -ignore extended custom matcher
272
+ Body : expect . toHaveSameHashAsBuffer ( firstBuffer ) ,
256
273
PartNumber : 1 ,
257
274
UploadId : "mockuploadId" ,
258
275
} ) ;
259
-
260
276
expect ( uploadPartMock ) . toHaveBeenNthCalledWith ( 2 , {
261
277
...actionParams ,
262
- Body : secondBuffer ,
278
+ // @ts -ignore extended custom matcher
279
+ Body : expect . toHaveSameHashAsBuffer ( secondBuffer ) ,
263
280
PartNumber : 2 ,
264
281
UploadId : "mockuploadId" ,
265
282
} ) ;
266
-
267
283
// complete multipart upload is called correctly.
268
284
expect ( completeMultipartMock ) . toHaveBeenCalledTimes ( 1 ) ;
269
285
expect ( completeMultipartMock ) . toHaveBeenLastCalledWith ( {
@@ -320,14 +336,16 @@ describe(Upload.name, () => {
320
336
expect ( uploadPartMock ) . toHaveBeenCalledTimes ( 2 ) ;
321
337
expect ( uploadPartMock ) . toHaveBeenNthCalledWith ( 1 , {
322
338
...actionParams ,
323
- Body : firstBuffer ,
339
+ // @ts -ignore extended custom matcher
340
+ Body : expect . toHaveSameHashAsBuffer ( firstBuffer ) ,
324
341
PartNumber : 1 ,
325
342
UploadId : "mockuploadId" ,
326
343
} ) ;
327
344
328
345
expect ( uploadPartMock ) . toHaveBeenNthCalledWith ( 2 , {
329
346
...actionParams ,
330
- Body : secondBuffer ,
347
+ // @ts -ignore extended custom matcher
348
+ Body : expect . toHaveSameHashAsBuffer ( secondBuffer ) ,
331
349
PartNumber : 2 ,
332
350
UploadId : "mockuploadId" ,
333
351
} ) ;
@@ -418,6 +436,8 @@ describe(Upload.name, () => {
418
436
expect ( putObjectTaggingMock ) . toHaveBeenCalledTimes ( 1 ) ;
419
437
expect ( putObjectTaggingMock ) . toHaveBeenCalledWith ( {
420
438
...actionParams ,
439
+ // @ts -ignore extended custom matcher
440
+ Body : expect . toHaveSameHashAsBuffer ( largeBuffer ) ,
421
441
Tagging : {
422
442
TagSet : tags ,
423
443
} ,
0 commit comments