Skip to content

Commit 7f2bd34

Browse files
committed
got some things working, now figuring out the general types in tests
1 parent f82c084 commit 7f2bd34

12 files changed

Lines changed: 234 additions & 127 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ crypt_shared.sh
109109

110110
*keytab
111111
driver.bundle.js
112+
test/tools/runner/bundle/

etc/build-runtime-barrel.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
// eslint-disable-next-line no-restricted-globals
6+
const useBundled = process.env.MONGODB_BUNDLED === 'true';
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
const rootDir = path.join(__dirname, '..');
10+
const outputBarrelFile = path.join(rootDir, 'test/mongodb_runtime-testing.ts');
11+
const source = useBundled ? './mongodb_bundled' : './mongodb';
12+
13+
const contents =
14+
`// This file is auto-generated. Do not edit.\n` +
15+
`// Run 'npm run build:runtime-barrel' to regenerate.\n` +
16+
`export const runNodelessTests = ${useBundled};\n` +
17+
`export * from '${source}';\n`;
18+
await fs.writeFile(outputBarrelFile, contents);
19+
20+
// eslint-disable-next-line no-console
21+
console.log(`✓ ${outputBarrelFile} now re-exports from ${source}`);

etc/bundle-driver.mjs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
#!/usr/bin/env node
2-
import * as esbuild from 'esbuild';
3-
import { fileURLToPath } from 'node:url';
2+
import fs from 'node:fs/promises';
43
import { isBuiltin } from 'node:module';
54
import path from 'node:path';
5+
import { fileURLToPath } from 'node:url';
6+
7+
import * as esbuild from 'esbuild';
68

79
const __dirname = path.dirname(fileURLToPath(import.meta.url));
810
const rootDir = path.join(__dirname, '..');
911

12+
const outdir = path.join(rootDir, 'test/tools/runner/bundle/');
13+
await fs.rm(outdir, { recursive: true, force: true });
14+
15+
const outputBundleFile = path.join(outdir, 'driver-bundle.js');
1016
await esbuild.build({
11-
entryPoints: [path.join(rootDir, 'src/index.ts')],
12-
bundle: true,
13-
outfile: path.join(rootDir, 'test/tools/runner/driver.bundle.js'),
14-
platform: 'node',
15-
format: 'cjs',
16-
target: 'node20',
17-
external: [
18-
'bson',
19-
'mongodb-connection-string-url',
20-
'@mongodb-js/saslprep',
21-
'@mongodb-js/zstd',
22-
'mongodb-client-encryption',
23-
'snappy',
24-
'@napi-rs/snappy*',
25-
'kerberos',
26-
'gcp-metadata',
27-
'@aws-sdk/credential-providers'
28-
],
29-
plugins: [{
30-
name: 'externalize-node-builtins',
31-
setup(build) {
32-
build.onResolve({ filter: /.*/ }, args => {
33-
if (isBuiltin(args.path)) {
34-
return { path: args.path, external: true };
35-
}
36-
});
37-
}
38-
}],
39-
sourcemap: 'inline',
40-
logLevel: 'info'
17+
entryPoints: [path.join(rootDir, 'test/mongodb.ts')],
18+
bundle: true,
19+
outfile: outputBundleFile,
20+
platform: 'node',
21+
format: 'cjs',
22+
target: 'node20',
23+
external: [
24+
'bson',
25+
'mongodb-connection-string-url',
26+
'@mongodb-js/saslprep',
27+
'@mongodb-js/zstd',
28+
'mongodb-client-encryption',
29+
'snappy',
30+
'@napi-rs/snappy*',
31+
'kerberos',
32+
'gcp-metadata',
33+
'@aws-sdk/credential-providers'
34+
],
35+
plugins: [
36+
{
37+
name: 'externalize-node-builtins',
38+
setup(build) {
39+
build.onResolve({ filter: /.*/ }, args => {
40+
if (isBuiltin(args.path)) {
41+
return { path: args.path, external: true };
42+
}
43+
});
44+
}
45+
}
46+
],
47+
sourcemap: 'inline',
48+
logLevel: 'info'
4149
});
4250

43-
console.log('✓ Driver bundle created at test/tools/runner/driver.bundle.js');
51+
// eslint-disable-next-line no-console
52+
console.log(`✓ Driver bundle created at ${outputBundleFile}`);

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"check:search-indexes": "nyc mocha --config test/mocha_mongodb.js test/manual/search-index-management.prose.test.ts",
143143
"check:test": "mocha --config test/mocha_mongodb.js test/integration",
144144
"check:unit": "nyc mocha test/unit",
145+
"check:unit-bundled": "MONGODB_BUNDLED=true npm run build:bundle && npm run check:unit",
145146
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
146147
"check:atlas": "nyc mocha --config test/manual/mocharc.js test/manual/atlas_connectivity.test.ts",
147148
"check:drivers-atlas-testing": "nyc mocha --config test/mocha_mongodb.js test/atlas/drivers_atlas_testing.test.ts",
@@ -155,9 +156,12 @@
155156
"check:csfle": "nyc mocha --config test/mocha_mongodb.js test/integration/client-side-encryption",
156157
"check:snappy": "nyc mocha test/unit/assorted/snappy.test.js",
157158
"check:x509": "nyc mocha test/manual/x509_auth.test.ts",
158-
"check:runtime-independence": "ts-node test/tools/runner/vm_runner.ts test/integration/change-streams/change_stream.test.ts",
159+
"check:runtime-independence": "ts-node test/tools/runner/vm_context_helper.ts test/integration/change-streams/change_stream.test.ts",
160+
"check:test-bundled": "MONGODB_BUNDLED=true npm run build:bundle && npm run check:test",
161+
"build:bundle": "npm run bundle:driver && npm run bundle:types && npm run build:runtime-barrel",
162+
"build:runtime-barrel": "node etc/build-runtime-barrel.mjs",
159163
"bundle:driver": "node etc/bundle-driver.mjs",
160-
"test:bundled": "npm run bundle:driver && npm run check:test",
164+
"bundle:types": "npx tsc --project ./tsconfig.json --declaration --emitDeclarationOnly --declarationDir test/tools/runner/bundle/types",
161165
"fix:eslint": "npm run check:eslint -- --fix",
162166
"prepare": "node etc/prepare.js",
163167
"preview:docs": "ts-node etc/docs/preview.ts",
@@ -174,4 +178,4 @@
174178
"moduleResolution": "node"
175179
}
176180
}
177-
}
181+
}

test/integration/crud/crud_api.test.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,28 @@ import { finished } from 'node:stream/promises';
33
import { expect } from 'chai';
44
import * as sinon from 'sinon';
55

6-
import { loadContextifiedMongoDBModule } from '../../tools/runner/vm_context_helper';
7-
import { type FailCommandFailPoint } from '../../tools/utils';
8-
import { assert as test } from '../shared';
9-
10-
// Load MongoDB module in VM context
11-
const mongodb = loadContextifiedMongoDBModule();
12-
13-
// Extract the exports we need from the contextified module
14-
const {
6+
import {
157
Collection,
168
CommandFailedEvent,
9+
type CommandStartedEvent,
1710
CommandSucceededEvent,
11+
type Db,
1812
MongoBulkWriteError,
19-
MongoClient,
13+
type MongoClient,
2014
MongoServerError,
2115
ObjectId,
22-
ReturnDocument
23-
} = mongodb;
24-
25-
type MongoClient = typeof mongodb.MongoClient.prototype;
26-
type Db = typeof mongodb.Db.prototype;
27-
type CommandStartedEvent = typeof mongodb.CommandStartedEvent.prototype;
16+
ReturnDocument,
17+
runNodelessTests
18+
} from '../../mongodb_runtime-testing';
19+
import { ensureTypeByName, type FailCommandFailPoint } from '../../tools/utils';
20+
import { assert as test } from '../shared';
2821

2922
const DB_NAME = 'crud_api_tests';
3023

3124
describe.only('CRUD API', function () {
3225
let client: MongoClient;
3326

3427
beforeEach(async function () {
35-
this.configuration.mongodb = mongodb;
36-
3728
client = this.configuration.newClient();
3829

3930
client.s.options.dbName = DB_NAME; // setup the default db
@@ -113,9 +104,13 @@ describe.only('CRUD API', function () {
113104
const spy = sinon.spy(Collection.prototype, 'find');
114105
const result = await collection.findOne({});
115106
expect(result).to.deep.equal({ _id: 1 });
116-
expect(events.at(0)).to.be.instanceOf(CommandSucceededEvent);
117-
expect(spy.returnValues.at(0)).to.have.property('closed', true);
118-
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
107+
if (runNodelessTests) {
108+
expect(ensureTypeByName(events.at(0), 'CommandSucceededEvent')).to.be.true;
109+
} else {
110+
expect(events.at(0)).to.be.instanceOf(CommandSucceededEvent);
111+
expect(spy.returnValues.at(0)).to.have.property('closed', true);
112+
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
113+
}
119114
});
120115
});
121116

@@ -145,10 +140,14 @@ describe.only('CRUD API', function () {
145140
it('the cursor for findOne is closed', async function () {
146141
const spy = sinon.spy(Collection.prototype, 'find');
147142
const error = await collection.findOne({}).catch(error => error);
148-
expect(error).to.be.instanceOf(MongoServerError);
149-
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
150-
expect(spy.returnValues.at(0)).to.have.property('closed', true);
151-
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
143+
if (runNodelessTests) {
144+
expect(ensureTypeByName(error, 'MongoServerError')).to.be.true;
145+
} else {
146+
expect(error).to.be.instanceOf(MongoServerError);
147+
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
148+
expect(spy.returnValues.at(0)).to.have.property('closed', true);
149+
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
150+
}
152151
});
153152
});
154153
});
@@ -183,9 +182,13 @@ describe.only('CRUD API', function () {
183182
const spy = sinon.spy(Collection.prototype, 'aggregate');
184183
const result = await collection.countDocuments({});
185184
expect(result).to.deep.equal(2);
186-
expect(events[0]).to.be.instanceOf(CommandSucceededEvent);
187-
expect(spy.returnValues[0]).to.have.property('closed', true);
188-
expect(spy.returnValues[0]).to.have.nested.property('session.hasEnded', true);
185+
if (runNodelessTests) {
186+
expect(ensureTypeByName(events[0], 'CommandSucceededEvent')).to.be.true;
187+
} else {
188+
expect(events[0]).to.be.instanceOf(CommandSucceededEvent);
189+
expect(spy.returnValues[0]).to.have.property('closed', true);
190+
expect(spy.returnValues[0]).to.have.nested.property('session.hasEnded', true);
191+
}
189192
});
190193
});
191194

@@ -215,10 +218,14 @@ describe.only('CRUD API', function () {
215218
it('the cursor for countDocuments is closed', async function () {
216219
const spy = sinon.spy(Collection.prototype, 'aggregate');
217220
const error = await collection.countDocuments({}).catch(error => error);
218-
expect(error).to.be.instanceOf(MongoServerError);
219-
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
220-
expect(spy.returnValues.at(0)).to.have.property('closed', true);
221-
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
221+
if (runNodelessTests) {
222+
expect(ensureTypeByName(error, 'MongoServerError')).to.be.true;
223+
} else {
224+
expect(error).to.be.instanceOf(MongoServerError);
225+
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
226+
expect(spy.returnValues.at(0)).to.have.property('closed', true);
227+
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
228+
}
222229
});
223230
});
224231
});
@@ -795,7 +802,11 @@ describe.only('CRUD API', function () {
795802
.bulkWrite(ops, { ordered: false, writeConcern: { w: 1 } })
796803
.catch(error => error);
797804

798-
expect(error).to.be.instanceOf(MongoBulkWriteError);
805+
if (runNodelessTests) {
806+
expect(ensureTypeByName(error, 'MongoBulkWriteError')).to.be.true;
807+
} else {
808+
expect(error).to.be.instanceOf(MongoBulkWriteError);
809+
}
799810
// 1004 because one of them is duplicate key
800811
// but since it is unordered we continued to write
801812
expect(error).to.have.property('insertedCount', 1004);
@@ -818,7 +829,8 @@ describe.only('CRUD API', function () {
818829
.collection('t20_1')
819830
.bulkWrite(ops, { ordered: true, writeConcern: { w: 1 } })
820831
.catch(err => err);
821-
expect(err).to.be.instanceOf(MongoBulkWriteError);
832+
// expect(err).to.be.instanceOf(MongoBulkWriteError);
833+
expect(ensureTypeByName(err, 'MongoBulkWriteError')).to.be.true;
822834
});
823835

824836
describe('sort support', function () {
@@ -827,7 +839,6 @@ describe.only('CRUD API', function () {
827839
let collection: Collection;
828840

829841
beforeEach(async function () {
830-
this.configuration.mongodb = mongodb;
831842
client = this.configuration.newClient({ monitorCommands: true });
832843
events = [];
833844
client.on('commandStarted', commandStarted =>

test/mongodb.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export * from '../src/constants';
5858
export * from '../src/cursor/abstract_cursor';
5959
export * from '../src/cursor/aggregation_cursor';
6060
export * from '../src/cursor/change_stream_cursor';
61-
export * from '../src/cursor/find_cursor';
62-
export * from '../src/cursor/list_collections_cursor';
63-
export * from '../src/cursor/list_indexes_cursor';
6461
export * from '../src/cursor/client_bulk_write_cursor';
6562
export * from '../src/cursor/explainable_cursor';
6663
export * from '../src/cursor/find_cursor';
64+
export * from '../src/cursor/find_cursor';
6765
export * from '../src/cursor/list_collections_cursor';
66+
export * from '../src/cursor/list_collections_cursor';
67+
export * from '../src/cursor/list_indexes_cursor';
6868
export * from '../src/cursor/list_indexes_cursor';
6969
export * from '../src/cursor/list_search_indexes_cursor';
7070
export * from '../src/cursor/run_command_cursor';
@@ -77,17 +77,17 @@ export * from '../src/gridfs/download';
7777
export * from '../src/gridfs/index';
7878
export * from '../src/gridfs/upload';
7979
export * from '../src/mongo_client';
80-
export * from '../src/mongo_logger';
81-
export * from '../src/mongo_types';
82-
export * from '../src/operations/aggregate';
83-
export * from '../src/operations/client_bulk_write/command_builder';
84-
export * from '../src/operations/client_bulk_write/common';
8580
export * from '../src/mongo_client_auth_providers';
8681
export * from '../src/mongo_logger';
82+
export * from '../src/mongo_logger';
8783
export * from '../src/mongo_types';
84+
export * from '../src/mongo_types';
85+
export * from '../src/operations/aggregate';
8886
export * from '../src/operations/aggregate';
8987
export * from '../src/operations/client_bulk_write/client_bulk_write';
9088
export * from '../src/operations/client_bulk_write/command_builder';
89+
export * from '../src/operations/client_bulk_write/command_builder';
90+
export * from '../src/operations/client_bulk_write/common';
9191
export * from '../src/operations/client_bulk_write/common';
9292
export * from '../src/operations/client_bulk_write/executor';
9393
export * from '../src/operations/client_bulk_write/results_merger';

0 commit comments

Comments
 (0)