@@ -3,6 +3,7 @@ import * as BSON from '../register-bson';
3
3
import { sorted , byStrings } from './tools/utils' ;
4
4
import { readFile } from 'fs/promises' ;
5
5
import { resolve } from 'path' ;
6
+ import * as child_process from 'node:child_process' ;
6
7
7
8
const EXPECTED_EXPORTS = [
8
9
// This is our added web indicator not a real export but a small exception for this test.
@@ -41,6 +42,7 @@ const EXPECTED_EXPORTS = [
41
42
] ;
42
43
43
44
const EXPECTED_EJSON_EXPORTS = [ 'parse' , 'stringify' , 'serialize' , 'deserialize' ] ;
45
+ const NODE_MAJOR = Number ( process . versions . node . split ( '.' ) [ 0 ] ) ;
44
46
45
47
describe ( 'bson entrypoint' , ( ) => {
46
48
it ( 'should export all and only the expected keys in expected_exports' , ( ) => {
@@ -96,4 +98,30 @@ describe('bson entrypoint', () => {
96
98
expect ( pkg ) . nested . property ( 'exports.default.types' , './bson.d.ts' ) ;
97
99
} ) ;
98
100
} ) ;
101
+
102
+ function testSyncESMImport ( name , module ) {
103
+ return ( ) => {
104
+ const child = child_process . spawnSync (
105
+ 'node' ,
106
+ [ '--experimental-print-required-tla' , '--print' , `require('${ module } ')` ] ,
107
+ { encoding : 'utf-8' }
108
+ ) ;
109
+
110
+ expect (
111
+ child . status ,
112
+ `expected to be able to 'require' to import the ${ name } ESM because there should be no top-level await:\n` +
113
+ child . stderr
114
+ ) . to . equal ( 0 ) ;
115
+ } ;
116
+ }
117
+
118
+ for ( const test of [
119
+ it (
120
+ 'browser bundle does not use top-level await' ,
121
+ testSyncESMImport ( 'browser' , './lib/bson.mjs' )
122
+ ) ,
123
+ it ( 'node bundle does not use top-level await' , testSyncESMImport ( 'node' , './lib/bson.node.mjs' ) )
124
+ ] ) {
125
+ if ( NODE_MAJOR < 22 ) test . skip ( ) ;
126
+ }
99
127
} ) ;
0 commit comments