@@ -3,6 +3,7 @@ const EJSON = BSON.EJSON;
3
3
import * as vm from 'node:vm' ;
4
4
import { expect } from 'chai' ;
5
5
import { BSONVersionError , BSONRuntimeError } from '../../src' ;
6
+ import { BSONError } from '../register-bson' ;
6
7
7
8
// BSON types
8
9
const Binary = BSON . Binary ;
@@ -583,4 +584,42 @@ describe('Extended JSON', function () {
583
584
} )
584
585
) . to . throw ( BSONVersionError , / U n s u p p o r t e d B S O N v e r s i o n / i) ;
585
586
} ) ;
587
+
588
+ context ( 'Map objects' , function ( ) {
589
+ it ( 'serializes an empty Map object' , ( ) => {
590
+ const input = new Map ( ) ;
591
+ expect ( EJSON . stringify ( input ) ) . to . equal ( '{}' ) ;
592
+ } ) ;
593
+
594
+ it ( 'serializes a nested Map object' , ( ) => {
595
+ const input = new Map ( [
596
+ [
597
+ 'a' ,
598
+ new Map ( [
599
+ [ 'a' , 100 ] ,
600
+ [ 'b' , 200 ]
601
+ ] )
602
+ ]
603
+ ] ) ;
604
+
605
+ const str = EJSON . stringify ( input ) ;
606
+ expect ( str ) . to . equal ( '{"a":{"a":100,"b":200}}' ) ;
607
+ } ) ;
608
+
609
+ it ( 'serializes a Map with one string key' , ( ) => {
610
+ const input = new Map ( [ [ 'a' , 100 ] ] ) ;
611
+
612
+ const str = EJSON . stringify ( input ) ;
613
+ expect ( str ) . to . equal ( '{"a":100}' ) ;
614
+ } ) ;
615
+
616
+ it ( 'throws BSONError when passed Map object with non-string keys' , ( ) => {
617
+ const input : Map < number , unknown > = new Map ( [
618
+ [ 1 , 100 ] ,
619
+ [ 2 , 200 ]
620
+ ] ) ;
621
+
622
+ expect ( ( ) => EJSON . stringify ( input ) ) . to . throw ( BSONError ) ;
623
+ } ) ;
624
+ } ) ;
586
625
} ) ;
0 commit comments