-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdependency.test.ts
More file actions
173 lines (148 loc) · 5.8 KB
/
dependency.test.ts
File metadata and controls
173 lines (148 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { execSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { expect } from 'chai';
import { dependencies, peerDependencies, peerDependenciesMeta } from '../../package.json';
import { setDifference } from '../mongodb';
import { alphabetically, itInNodeProcess, sorted } from '../tools/utils';
const EXPECTED_DEPENDENCIES = sorted(
['@mongodb-js/saslprep', 'bson', 'mongodb-connection-string-url'],
alphabetically
);
const EXPECTED_PEER_DEPENDENCIES = [
'@aws-sdk/credential-providers',
'@mongodb-js/zstd',
'kerberos',
'snappy',
'mongodb-client-encryption',
'gcp-metadata',
'socks'
];
describe('package.json', function () {
describe('dependencies', function () {
it('only contains the expected dependencies', function () {
expect(sorted(Object.keys(dependencies), alphabetically)).to.deep.equal(
EXPECTED_DEPENDENCIES
);
});
});
describe('peerDependencies', function () {
it('only contains the expected peerDependencies', function () {
expect(peerDependencies).to.have.keys(EXPECTED_PEER_DEPENDENCIES);
});
it('has a meta field for each expected peer', function () {
expect(peerDependenciesMeta).to.have.keys(EXPECTED_PEER_DEPENDENCIES);
});
});
describe('Optional Peer dependencies', () => {
after('reset npm dependencies', () => {
// This test file is not meant to be run alongside others, but in case
// we can attempt to reset the environment
fs.rmSync(path.join(repoRoot, 'node_modules'), { recursive: true, force: true });
execSync(`npm clean-install`);
});
const repoRoot = path.resolve(__dirname, '../..');
const testScript = `
const mdb = require('${repoRoot}/src/index.ts');
console.log('import success!');`
.split('\n')
.join('');
for (const [depName, depVersion] of Object.entries(
peerDependencies as Record<string, string>
)) {
// If a dependency specifies `alpha|beta`, the major version will fail to install because
// an alpha < the major of that version (ex: [email protected] < [email protected])
const depInstallSpecifier = /alpha|beta/.test(depVersion)
? depVersion
: depVersion.split('.')[0];
context(`when ${depName} is NOT installed`, () => {
beforeEach(async () => {
fs.rmSync(path.join(repoRoot, 'node_modules', depName), { recursive: true, force: true });
});
it(`driver is importable`, () => {
expect(fs.existsSync(path.join(repoRoot, 'node_modules', depName))).to.be.false;
const result = execSync(`./node_modules/.bin/ts-node -e "${testScript}"`, {
encoding: 'utf8'
});
expect(result).to.include('import success!');
});
if (depName === 'snappy') {
itInNodeProcess('getSnappy returns rejected import', async function ({ expect }) {
// @ts-expect-error: import from the inside forked process
const { getSnappy } = await import('./src/deps.ts');
const snappyImport = getSnappy();
expect(snappyImport).to.have.nested.property(
'kModuleError.name',
'MongoMissingDependencyError'
);
});
}
});
context(`when ${depName} is installed`, () => {
beforeEach(async function () {
execSync(`npm install --no-save -D "${depName}"@"${depInstallSpecifier}"`);
});
it(`driver is importable`, () => {
expect(fs.existsSync(path.join(repoRoot, 'node_modules', depName))).to.be.true;
const result = execSync(`./node_modules/.bin/ts-node -e "${testScript}"`, {
encoding: 'utf8'
});
expect(result).to.include('import success!');
});
if (depName === 'snappy') {
itInNodeProcess('getSnappy returns fulfilled import', async function ({ expect }) {
// @ts-expect-error: import from the inside forked process
const { getSnappy } = await import('./src/deps.ts');
const snappyImport = getSnappy();
expect(snappyImport).to.have.property('compress').that.is.a('function');
expect(snappyImport).to.have.property('uncompress').that.is.a('function');
});
}
});
}
});
const EXPECTED_IMPORTS = [
'bson',
'@mongodb-js/saslprep',
'sparse-bitfield',
'memory-pager',
'mongodb-connection-string-url',
'whatwg-url',
'webidl-conversions',
'tr46',
'punycode'
];
describe('mongodb imports', () => {
let imports: string[];
beforeEach(async function () {
for (const key of Object.keys(require.cache)) delete require.cache[key];
// Intentionally import from `src` because we are testing the package's exports (index.ts)
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../../src');
imports = Array.from(
new Set(
Object.entries(require.cache)
.filter(([modKey]) => modKey.includes('/node_modules/'))
.map(([modKey]) => {
const leadingPkgName = modKey.split('/node_modules/')[1];
const [orgName, pkgName] = leadingPkgName.split('/');
if (orgName.startsWith('@')) {
return `${orgName}/${pkgName}`;
}
return orgName;
})
)
);
});
context('when importing mongodb', () => {
it('only contains the expected imports', function () {
expect(setDifference(imports, EXPECTED_IMPORTS)).to.deep.equal(new Set());
});
it('does not import optional dependencies', () => {
for (const peerDependency of EXPECTED_PEER_DEPENDENCIES) {
expect(imports).to.not.include(peerDependency);
}
});
});
});
});