-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdependency.test.ts
159 lines (135 loc) · 4.93 KB
/
dependency.test.ts
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
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 { itInNodeProcess } from '../tools/utils';
const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url'];
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(dependencies).to.have.keys(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)) {
const depMajor = 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, mongodb }) {
const snappyImport = mongodb.getSnappy();
expect(snappyImport).to.have.nested.property(
'kModuleError.name',
'MongoMissingDependencyError'
);
}
);
}
});
context(`when ${depName} is installed`, () => {
beforeEach(async () => {
execSync(`npm install --no-save "${depName}"@"${depMajor}"`);
});
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, mongodb }) {
const snappyImport = mongodb.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',
'saslprep',
'sparse-bitfield',
'memory-pager',
'mongodb-connection-string-url',
'whatwg-url',
'webidl-conversions',
'tr46'
];
describe('mongodb imports', () => {
let imports: string[];
beforeEach(async function () {
for (const key of Object.keys(require.cache)) delete require.cache[key];
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(imports).to.deep.equal(EXPECTED_IMPORTS);
});
it('does not import optional dependencies', () => {
for (const peerDependency of EXPECTED_PEER_DEPENDENCIES) {
expect(imports).to.not.include(peerDependency);
}
});
});
});
});