1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
import { expect } from 'chai' ;
2
3
import { inspect } from 'util' ;
3
4
7
8
Document ,
8
9
Long ,
9
10
MongoError ,
11
+ MongoServerError ,
10
12
ObjectId ,
11
13
OneOrMore
12
14
} from '../../../src' ;
@@ -264,11 +266,11 @@ export function specialCheck(
264
266
entities : EntitiesMap ,
265
267
path : string [ ] = [ ] ,
266
268
checkExtraKeys : boolean
267
- ) : boolean {
269
+ ) : void {
268
270
if ( isUnsetOrMatchesOperator ( expected ) ) {
269
271
if ( actual === null || actual === undefined ) return ;
270
272
271
- resultCheck ( actual , expected . $$unsetOrMatches , entities , path , checkExtraKeys ) ;
273
+ resultCheck ( actual , expected . $$unsetOrMatches as any , entities , path , checkExtraKeys ) ;
272
274
} else if ( isMatchesEntityOperator ( expected ) ) {
273
275
// $$matchesEntity
274
276
const entity = entities . get ( expected . $$matchesEntity ) ;
@@ -290,15 +292,15 @@ export function specialCheck(
290
292
// $$sessionLsid
291
293
const session = entities . getEntity ( 'session' , expected . $$sessionLsid , false ) ;
292
294
expect ( session , `Session ${ expected . $$sessionLsid } does not exist in entities` ) . to . exist ;
293
- const entitySessionHex = session . id . id . buffer . toString ( 'hex' ) . toUpperCase ( ) ;
295
+ const entitySessionHex = session . id ! . id . buffer . toString ( 'hex' ) . toUpperCase ( ) ;
294
296
const actualSessionHex = actual . id . buffer . toString ( 'hex' ) . toUpperCase ( ) ;
295
297
expect (
296
298
entitySessionHex ,
297
299
`Session entity ${ expected . $$sessionLsid } does not match lsid`
298
300
) . to . equal ( actualSessionHex ) ;
299
301
} else if ( isTypeOperator ( expected ) ) {
300
302
// $$type
301
- let ok : boolean ;
303
+ let ok = false ;
302
304
const types = Array . isArray ( expected . $$type ) ? expected . $$type : [ expected . $$type ] ;
303
305
for ( const type of types ) {
304
306
ok ||= TYPE_MAP . get ( type ) ( actual ) ;
@@ -364,19 +366,23 @@ function compareCommandStartedEvents(
364
366
entities : EntitiesMap ,
365
367
prefix : string
366
368
) {
367
- if ( expected . command ) {
368
- resultCheck ( actual . command , expected . command , entities , [ `${ prefix } .command` ] ) ;
369
+ if ( expected ! . command ) {
370
+ resultCheck ( actual . command , expected ! . command , entities , [ `${ prefix } .command` ] ) ;
369
371
}
370
- if ( expected . commandName ) {
372
+ if ( expected ! . commandName ) {
371
373
expect (
372
- expected . commandName ,
373
- `expected ${ prefix } .commandName to equal ${ expected . commandName } but received ${ actual . commandName } `
374
+ expected ! . commandName ,
375
+ `expected ${ prefix } .commandName to equal ${ expected ! . commandName } but received ${
376
+ actual . commandName
377
+ } `
374
378
) . to . equal ( actual . commandName ) ;
375
379
}
376
- if ( expected . databaseName ) {
380
+ if ( expected ! . databaseName ) {
377
381
expect (
378
- expected . databaseName ,
379
- `expected ${ prefix } .databaseName to equal ${ expected . databaseName } but received ${ actual . databaseName } `
382
+ expected ! . databaseName ,
383
+ `expected ${ prefix } .databaseName to equal ${ expected ! . databaseName } but received ${
384
+ actual . databaseName
385
+ } `
380
386
) . to . equal ( actual . databaseName ) ;
381
387
}
382
388
}
@@ -387,13 +393,15 @@ function compareCommandSucceededEvents(
387
393
entities : EntitiesMap ,
388
394
prefix : string
389
395
) {
390
- if ( expected . reply ) {
391
- resultCheck ( actual . reply , expected . reply , entities , [ prefix ] ) ;
396
+ if ( expected ! . reply ) {
397
+ resultCheck ( actual . reply as Document , expected ! . reply , entities , [ prefix ] ) ;
392
398
}
393
- if ( expected . commandName ) {
399
+ if ( expected ! . commandName ) {
394
400
expect (
395
- expected . commandName ,
396
- `expected ${ prefix } .commandName to equal ${ expected . commandName } but received ${ actual . commandName } `
401
+ expected ! . commandName ,
402
+ `expected ${ prefix } .commandName to equal ${ expected ! . commandName } but received ${
403
+ actual . commandName
404
+ } `
397
405
) . to . equal ( actual . commandName ) ;
398
406
}
399
407
}
@@ -404,10 +412,12 @@ function compareCommandFailedEvents(
404
412
entities : EntitiesMap ,
405
413
prefix : string
406
414
) {
407
- if ( expected . commandName ) {
415
+ if ( expected ! . commandName ) {
408
416
expect (
409
- expected . commandName ,
410
- `expected ${ prefix } .commandName to equal ${ expected . commandName } but received ${ actual . commandName } `
417
+ expected ! . commandName ,
418
+ `expected ${ prefix } .commandName to equal ${ expected ! . commandName } but received ${
419
+ actual . commandName
420
+ } `
411
421
) . to . equal ( actual . commandName ) ;
412
422
}
413
423
}
@@ -489,28 +499,34 @@ export function matchesEvents(
489
499
}
490
500
}
491
501
502
+ function isMongoCryptError ( err ) : boolean {
503
+ if ( err . constructor . name === 'MongoCryptError' ) {
504
+ return true ;
505
+ }
506
+ return err . stack . includes ( 'at ClientEncryption' ) ;
507
+ }
508
+
492
509
export function expectErrorCheck (
493
510
error : Error | MongoError ,
494
511
expected : ExpectedError ,
495
512
entities : EntitiesMap
496
- ) : boolean {
497
- if ( Object . keys ( expected ) [ 0 ] === 'isClientError' || Object . keys ( expected ) [ 0 ] === 'isError' ) {
498
- // FIXME: We cannot tell if Error arose from driver and not from server
499
- return ;
513
+ ) : void {
514
+ const expectMessage = `\n\nOriginal Error Stack:\n${ error . stack } \n\n` ;
515
+
516
+ if ( ! isMongoCryptError ( error ) ) {
517
+ expect ( error , expectMessage ) . to . be . instanceOf ( MongoError ) ;
500
518
}
501
519
502
- const expectMessage = `\n\nOriginal Error Stack:\n${ error . stack } \n\n` ;
520
+ if ( expected . isClientError === false ) {
521
+ expect ( error ) . to . be . instanceOf ( MongoServerError ) ;
522
+ } else if ( expected . isClientError === true ) {
523
+ expect ( error ) . not . to . be . instanceOf ( MongoServerError ) ;
524
+ }
503
525
504
526
if ( expected . errorContains != null ) {
505
527
expect ( error . message , expectMessage ) . to . include ( expected . errorContains ) ;
506
528
}
507
529
508
- if ( ! ( error instanceof MongoError ) ) {
509
- // if statement asserts type for TS, expect will always fail
510
- expect ( error , expectMessage ) . to . be . instanceOf ( MongoError ) ;
511
- return ;
512
- }
513
-
514
530
if ( expected . errorCode != null ) {
515
531
expect ( error , expectMessage ) . to . have . property ( 'code' , expected . errorCode ) ;
516
532
}
@@ -520,24 +536,26 @@ export function expectErrorCheck(
520
536
}
521
537
522
538
if ( expected . errorLabelsContain != null ) {
539
+ const mongoError = error as MongoError ;
523
540
for ( const errorLabel of expected . errorLabelsContain ) {
524
541
expect (
525
- error . hasErrorLabel ( errorLabel ) ,
526
- `Error was supposed to have label ${ errorLabel } , has [${ error . errorLabels } ] -- ${ expectMessage } `
542
+ mongoError . hasErrorLabel ( errorLabel ) ,
543
+ `Error was supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ] -- ${ expectMessage } `
527
544
) . to . be . true ;
528
545
}
529
546
}
530
547
531
548
if ( expected . errorLabelsOmit != null ) {
549
+ const mongoError = error as MongoError ;
532
550
for ( const errorLabel of expected . errorLabelsOmit ) {
533
551
expect (
534
- error . hasErrorLabel ( errorLabel ) ,
535
- `Error was supposed to have label ${ errorLabel } , has [${ error . errorLabels } ] -- ${ expectMessage } `
552
+ mongoError . hasErrorLabel ( errorLabel ) ,
553
+ `Error was not supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ] -- ${ expectMessage } `
536
554
) . to . be . false ;
537
555
}
538
556
}
539
557
540
558
if ( expected . expectResult != null ) {
541
- resultCheck ( error , expected . expectResult , entities ) ;
559
+ resultCheck ( error , expected . expectResult as any , entities ) ;
542
560
}
543
561
}
0 commit comments