@@ -20,6 +20,7 @@ import {
20
20
ConnectionPoolCreatedEvent ,
21
21
ConnectionPoolReadyEvent ,
22
22
ConnectionReadyEvent ,
23
+ CSOTError ,
23
24
type Document ,
24
25
Long ,
25
26
MongoError ,
@@ -134,7 +135,19 @@ export function isSpecialOperator(value: unknown): value is SpecialOperator {
134
135
135
136
const TYPE_MAP = new Map ( ) ;
136
137
137
- TYPE_MAP . set ( 'double' , actual => typeof actual === 'number' || actual . _bsontype === 'Double' ) ;
138
+ function hasBSONType ( value : unknown ) : value is { _bsontype : string } & Record < string , any > {
139
+ return (
140
+ typeof value === 'object' &&
141
+ value != null &&
142
+ '_bsontype' in value &&
143
+ typeof value . _bsontype === 'string'
144
+ ) ;
145
+ }
146
+
147
+ TYPE_MAP . set (
148
+ 'double' ,
149
+ actual => typeof actual === 'number' || ( hasBSONType ( actual ) && actual . _bsontype === 'Double' )
150
+ ) ;
138
151
TYPE_MAP . set ( 'string' , actual => typeof actual === 'string' ) ;
139
152
TYPE_MAP . set ( 'object' , actual => typeof actual === 'object' && actual !== null ) ;
140
153
TYPE_MAP . set ( 'array' , actual => Array . isArray ( actual ) ) ;
@@ -144,18 +157,26 @@ TYPE_MAP.set('objectId', actual => actual instanceof ObjectId);
144
157
TYPE_MAP . set ( 'bool' , actual => typeof actual === 'boolean' ) ;
145
158
TYPE_MAP . set ( 'date' , actual => actual instanceof Date ) ;
146
159
TYPE_MAP . set ( 'null' , actual => actual === null ) ;
147
- TYPE_MAP . set ( 'regex' , actual => actual instanceof RegExp || actual . _bsontype === 'BSONRegExp' ) ;
148
- TYPE_MAP . set ( 'dbPointer' , actual => actual . _bsontype === 'DBRef' ) ;
149
- TYPE_MAP . set ( 'javascript' , actual => actual . _bsontype === 'Code' ) ;
150
- TYPE_MAP . set ( 'symbol' , actual => actual . _bsontype === 'Symbol' ) ;
151
- TYPE_MAP . set ( 'javascriptWithScope' , actual => actual . _bsontype === 'Code' && actual . scope ) ;
152
- TYPE_MAP . set ( 'timestamp' , actual => actual . _bsontype === 'Timestamp' ) ;
153
- TYPE_MAP . set ( 'decimal' , actual => actual . _bsontype === 'Decimal128' ) ;
154
- TYPE_MAP . set ( 'minKey' , actual => actual . _bsontype === 'MinKey' ) ;
155
- TYPE_MAP . set ( 'maxKey' , actual => actual . _bsontype === 'MaxKey' ) ;
160
+ TYPE_MAP . set (
161
+ 'regex' ,
162
+ actual => actual instanceof RegExp || ( hasBSONType ( actual ) && actual . _bsontype === 'BSONRegExp' )
163
+ ) ;
164
+ TYPE_MAP . set ( 'dbPointer' , actual => hasBSONType ( actual ) && actual . _bsontype === 'DBRef' ) ;
165
+ TYPE_MAP . set ( 'javascript' , actual => hasBSONType ( actual ) && actual . _bsontype === 'Code' ) ;
166
+ TYPE_MAP . set ( 'symbol' , actual => hasBSONType ( actual ) && actual . _bsontype === 'Symbol' ) ;
167
+ TYPE_MAP . set (
168
+ 'javascriptWithScope' ,
169
+ actual => hasBSONType ( actual ) && actual . _bsontype === 'Code' && actual . scope
170
+ ) ;
171
+ TYPE_MAP . set ( 'timestamp' , actual => hasBSONType ( actual ) && actual . _bsontype === 'Timestamp' ) ;
172
+ TYPE_MAP . set ( 'decimal' , actual => hasBSONType ( actual ) && actual . _bsontype === 'Decimal128' ) ;
173
+ TYPE_MAP . set ( 'minKey' , actual => hasBSONType ( actual ) && actual . _bsontype === 'MinKey' ) ;
174
+ TYPE_MAP . set ( 'maxKey' , actual => hasBSONType ( actual ) && actual . _bsontype === 'MaxKey' ) ;
156
175
TYPE_MAP . set (
157
176
'int' ,
158
- actual => ( typeof actual === 'number' && Number . isInteger ( actual ) ) || actual . _bsontype === 'Int32'
177
+ actual =>
178
+ ( typeof actual === 'number' && Number . isInteger ( actual ) ) ||
179
+ ( hasBSONType ( actual ) && actual . _bsontype === 'Int32' )
159
180
) ;
160
181
TYPE_MAP . set (
161
182
'long' ,
@@ -342,7 +363,7 @@ export function specialCheck(
342
363
for ( const type of types ) {
343
364
ok ||= TYPE_MAP . get ( type ) ( actual ) ;
344
365
}
345
- expect ( ok , `Expected [${ actual } ] to be one of [${ types } ]` ) . to . be . true ;
366
+ expect ( ok , `Expected [${ actual } ] to be one of [${ types } ] at ${ path . join ( '' ) } ` ) . to . be . true ;
346
367
} else if ( isExistsOperator ( expected ) ) {
347
368
// $$exists
348
369
const actualExists = actual !== undefined && actual !== null ;
@@ -728,6 +749,10 @@ export function expectErrorCheck(
728
749
expect ( error , expectMessage ) . to . be . instanceOf ( MongoError ) ;
729
750
}
730
751
752
+ if ( expected . isTimeoutError === true ) {
753
+ expect ( CSOTError . is ( error ) , error . stack ) . to . be . true ;
754
+ }
755
+
731
756
if ( expected . isClientError === false ) {
732
757
expect ( error ) . to . be . instanceOf ( MongoServerError ) ;
733
758
} else if ( expected . isClientError === true ) {
@@ -741,19 +766,19 @@ export function expectErrorCheck(
741
766
}
742
767
743
768
if ( expected . errorCode != null ) {
744
- expect ( error , expectMessage ) . to . have . property ( 'code' , expected . errorCode ) ;
769
+ expect ( error ) . to . have . property ( 'code' , expected . errorCode ) ;
745
770
}
746
771
747
772
if ( expected . errorCodeName != null ) {
748
- expect ( error , expectMessage ) . to . have . property ( 'codeName' , expected . errorCodeName ) ;
773
+ expect ( error ) . to . have . property ( 'codeName' , expected . errorCodeName ) ;
749
774
}
750
775
751
776
if ( expected . errorLabelsContain != null ) {
752
777
const mongoError = error as MongoError ;
753
778
for ( const errorLabel of expected . errorLabelsContain ) {
754
779
expect (
755
780
mongoError . hasErrorLabel ( errorLabel ) ,
756
- `Error was supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ] -- ${ expectMessage } `
781
+ `Error was supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ]`
757
782
) . to . be . true ;
758
783
}
759
784
}
@@ -763,7 +788,7 @@ export function expectErrorCheck(
763
788
for ( const errorLabel of expected . errorLabelsOmit ) {
764
789
expect (
765
790
mongoError . hasErrorLabel ( errorLabel ) ,
766
- `Error was not supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ] -- ${ expectMessage } `
791
+ `Error was not supposed to have label ${ errorLabel } , has [${ mongoError . errorLabels } ]`
767
792
) . to . be . false ;
768
793
}
769
794
}
0 commit comments