@@ -5,6 +5,7 @@ import * as path from 'node:path';
55import { expect } from 'chai' ;
66
77import { dependencies , peerDependencies , peerDependenciesMeta } from '../../package.json' ;
8+ import { itInNodeProcess } from '../tools/utils' ;
89
910const EXPECTED_DEPENDENCIES = [ 'bson' , 'mongodb-connection-string-url' , 'socks' ] ;
1011const EXPECTED_PEER_DEPENDENCIES = [
@@ -65,11 +66,24 @@ describe('package.json', function () {
6566
6667 expect ( result ) . to . include ( 'import success!' ) ;
6768 } ) ;
69+
70+ if ( depName === 'snappy' ) {
71+ itInNodeProcess (
72+ 'getSnappy returns rejected import' ,
73+ async function ( { expect, mongodb } ) {
74+ const snappyImport = mongodb . getSnappy ( ) ;
75+ expect ( snappyImport ) . to . have . nested . property (
76+ 'kModuleError.name' ,
77+ 'MongoMissingDependencyError'
78+ ) ;
79+ }
80+ ) ;
81+ }
6882 } ) ;
6983
7084 context ( `when ${ depName } is installed` , ( ) => {
7185 beforeEach ( async ( ) => {
72- execSync ( `npm install --no-save "${ depName } "@${ depMajor } ` ) ;
86+ execSync ( `npm install --no-save "${ depName } "@" ${ depMajor } " ` ) ;
7387 } ) ;
7488
7589 it ( `driver is importable` , ( ) => {
@@ -81,7 +95,66 @@ describe('package.json', function () {
8195
8296 expect ( result ) . to . include ( 'import success!' ) ;
8397 } ) ;
98+
99+ if ( depName === 'snappy' ) {
100+ itInNodeProcess (
101+ 'getSnappy returns fulfilled import' ,
102+ async function ( { expect, mongodb } ) {
103+ const snappyImport = mongodb . getSnappy ( ) ;
104+ expect ( snappyImport ) . to . have . property ( 'compress' ) . that . is . a ( 'function' ) ;
105+ expect ( snappyImport ) . to . have . property ( 'uncompress' ) . that . is . a ( 'function' ) ;
106+ }
107+ ) ;
108+ }
84109 } ) ;
85110 }
86111 } ) ;
112+
113+ const EXPECTED_IMPORTS = [
114+ 'bson' ,
115+ 'saslprep' ,
116+ 'sparse-bitfield' ,
117+ 'memory-pager' ,
118+ 'mongodb-connection-string-url' ,
119+ 'whatwg-url' ,
120+ 'webidl-conversions' ,
121+ 'tr46' ,
122+ 'socks' ,
123+ 'ip' ,
124+ 'smart-buffer'
125+ ] ;
126+
127+ describe ( 'mongodb imports' , ( ) => {
128+ let imports : string [ ] ;
129+ beforeEach ( async function ( ) {
130+ for ( const key of Object . keys ( require . cache ) ) delete require . cache [ key ] ;
131+ require ( '../../src' ) ;
132+ imports = Array . from (
133+ new Set (
134+ Object . entries ( require . cache )
135+ . filter ( ( [ modKey ] ) => modKey . includes ( '/node_modules/' ) )
136+ . map ( ( [ modKey ] ) => {
137+ const leadingPkgName = modKey . split ( '/node_modules/' ) [ 1 ] ;
138+ const [ orgName , pkgName ] = leadingPkgName . split ( '/' ) ;
139+ if ( orgName . startsWith ( '@' ) ) {
140+ return `${ orgName } /${ pkgName } ` ;
141+ }
142+ return orgName ;
143+ } )
144+ )
145+ ) ;
146+ } ) ;
147+
148+ context ( 'when importing mongodb' , ( ) => {
149+ it ( 'only contains the expected imports' , function ( ) {
150+ expect ( imports ) . to . deep . equal ( EXPECTED_IMPORTS ) ;
151+ } ) ;
152+
153+ it ( 'does not import optional dependencies' , ( ) => {
154+ for ( const peerDependency of EXPECTED_PEER_DEPENDENCIES ) {
155+ expect ( imports ) . to . not . include ( peerDependency ) ;
156+ }
157+ } ) ;
158+ } ) ;
159+ } ) ;
87160} ) ;
0 commit comments