@@ -5,6 +5,7 @@ import * as path from 'node:path';
5
5
import { expect } from 'chai' ;
6
6
7
7
import { dependencies , peerDependencies , peerDependenciesMeta } from '../../package.json' ;
8
+ import { itInNodeProcess } from '../tools/utils' ;
8
9
9
10
const EXPECTED_DEPENDENCIES = [ 'bson' , 'mongodb-connection-string-url' , 'socks' ] ;
10
11
const EXPECTED_PEER_DEPENDENCIES = [
@@ -65,11 +66,24 @@ describe('package.json', function () {
65
66
66
67
expect ( result ) . to . include ( 'import success!' ) ;
67
68
} ) ;
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
+ }
68
82
} ) ;
69
83
70
84
context ( `when ${ depName } is installed` , ( ) => {
71
85
beforeEach ( async ( ) => {
72
- execSync ( `npm install --no-save "${ depName } "@${ depMajor } ` ) ;
86
+ execSync ( `npm install --no-save "${ depName } "@" ${ depMajor } " ` ) ;
73
87
} ) ;
74
88
75
89
it ( `driver is importable` , ( ) => {
@@ -81,7 +95,66 @@ describe('package.json', function () {
81
95
82
96
expect ( result ) . to . include ( 'import success!' ) ;
83
97
} ) ;
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
+ }
84
109
} ) ;
85
110
}
86
111
} ) ;
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
+ } ) ;
87
160
} ) ;
0 commit comments