@@ -3,6 +3,7 @@ const EJSON = BSON.EJSON;
33import * as vm from 'node:vm' ;
44import { expect } from 'chai' ;
55import { BSONVersionError , BSONRuntimeError } from '../../src' ;
6+ import { BSONError } from '../register-bson' ;
67
78// BSON types
89const Binary = BSON . Binary ;
@@ -583,4 +584,42 @@ describe('Extended JSON', function () {
583584 } )
584585 ) . 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) ;
585586 } ) ;
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+ } ) ;
586625} ) ;
0 commit comments