Skip to content

Commit eb7e1bc

Browse files
committed
test(NODE-6920): esm bundles do not have top-level await
1 parent 4602973 commit eb7e1bc

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"exports": {
8080
"browser": {
8181
"types": "./bson.d.ts",
82-
"default": "./lib/bson.browser.mjs"
82+
"default": "./lib/bson.mjs"
8383
},
8484
"react-native": "./lib/bson.rn.cjs",
8585
"default": {

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const config = [
6161
nodeResolve({ resolveOnly: [] })
6262
],
6363
output: {
64-
file: 'lib/bson.browser.mjs',
64+
file: 'lib/bson.mjs',
6565
format: 'esm',
6666
sourcemap: true
6767
}

test/node/exports.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as BSON from '../register-bson';
33
import { sorted, byStrings } from './tools/utils';
44
import { readFile } from 'fs/promises';
55
import { resolve } from 'path';
6+
import * as child_process from 'node:child_process';
67

78
const EXPECTED_EXPORTS = [
89
// This is our added web indicator not a real export but a small exception for this test.
@@ -41,6 +42,7 @@ const EXPECTED_EXPORTS = [
4142
];
4243

4344
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];
45+
const NODE_MAJOR = Number(process.versions.node.split('.')[0]);
4446

4547
describe('bson entrypoint', () => {
4648
it('should export all and only the expected keys in expected_exports', () => {
@@ -96,4 +98,30 @@ describe('bson entrypoint', () => {
9698
expect(pkg).nested.property('exports.default.types', './bson.d.ts');
9799
});
98100
});
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+
}
99127
});

0 commit comments

Comments
 (0)