Skip to content

Commit c169e58

Browse files
spy instead of injecting
1 parent 46e779d commit c169e58

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

src/cmap/wire_protocol/on_demand/document.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
Binary,
3-
BSON,
43
type BSONElement,
54
BSONError,
65
type BSONSerializeOptions,
76
BSONType,
7+
deserialize,
88
getBigInt64LE,
99
getFloat64LE,
1010
getInt32LE,
@@ -46,13 +46,6 @@ type CachedBSONElement = { element: BSONElement; value: any | undefined };
4646

4747
/** @internal */
4848
export class OnDemandDocument {
49-
/**
50-
* @internal
51-
*
52-
* Used for testing purposes.
53-
*/
54-
private static BSON: typeof BSON = BSON;
55-
5649
/**
5750
* Maps JS strings to elements and jsValues for speeding up subsequent lookups.
5851
* - If `false` then name does not exist in the BSON document
@@ -344,7 +337,7 @@ export class OnDemandDocument {
344337
index: this.offset,
345338
allowObjectSmallerThanBufferSize: true
346339
};
347-
return OnDemandDocument.BSON.deserialize(this.bson, exactBSONOptions);
340+
return deserialize(this.bson, exactBSONOptions);
348341
}
349342

350343
private parseBsonSerializationOptions(options?: { enableUtf8Validation?: boolean }): {

test/integration/node-specific/bson-options/utf8_validation.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
type Collection,
1010
deserialize,
1111
type MongoClient,
12-
MongoDBResponse,
1312
MongoServerError,
13+
OnDemandDocument,
1414
OpMsgResponse
1515
} from '../../../mongodb';
1616

@@ -28,12 +28,12 @@ describe('class MongoDBResponse', () => {
2828
let bsonSpy: sinon.SinonSpy;
2929

3030
beforeEach(() => {
31-
bsonSpy = sinon.spy(MongoDBResponse.prototype, 'parseBsonSerializationOptions');
31+
// @ts-expect-error private function
32+
bsonSpy = sinon.spy(OnDemandDocument.prototype, 'parseBsonSerializationOptions');
3233
});
3334

3435
afterEach(() => {
3536
bsonSpy?.restore();
36-
// @ts-expect-error: Allow this to be garbage collected
3737
bsonSpy = null;
3838
});
3939

@@ -159,7 +159,7 @@ describe('class MongoDBResponse', () => {
159159
);
160160
});
161161

162-
describe('utf8 validation with cursors' + i, function () {
162+
describe('utf8 validation with cursors', function () {
163163
let client: MongoClient;
164164
let collection: Collection;
165165

test/unit/cmap/wire_protocol/responses.test.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import * as SPYABLE_BSON from 'bson';
21
import { expect } from 'chai';
32
import * as sinon from 'sinon';
43

4+
// to spy on the bson module, we must import it from the driver
5+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
6+
import * as mdb from '../../../../src/bson';
57
import {
6-
BSON,
78
CursorResponse,
89
Int32,
910
MongoDBResponse,
1011
MongoUnexpectedServerResponseError,
11-
OnDemandDocument
12+
OnDemandDocument,
13+
serialize
1214
} from '../../../mongodb';
1315

1416
describe('class MongoDBResponse', () => {
1517
it('is a subclass of OnDemandDocument', () => {
16-
expect(new MongoDBResponse(BSON.serialize({ ok: 1 }))).to.be.instanceOf(OnDemandDocument);
18+
expect(new MongoDBResponse(serialize({ ok: 1 }))).to.be.instanceOf(OnDemandDocument);
1719
});
1820

1921
context('utf8 validation', () => {
20-
let deseriailzeSpy: sinon.SinonSpy;
22+
let deseriailzeSpy: sinon.SinonStub<Parameters<typeof mdb.deserialize>>;
2123
beforeEach(function () {
22-
// @ts-expect-error accessing internal property.
23-
OnDemandDocument.BSON = SPYABLE_BSON;
24-
25-
deseriailzeSpy = sinon.spy(SPYABLE_BSON, 'deserialize');
24+
const deserialize = mdb.deserialize;
25+
deseriailzeSpy = sinon.stub<Parameters<typeof deserialize>>().callsFake(deserialize);
26+
sinon.stub(mdb, 'deserialize').get(() => {
27+
return deseriailzeSpy;
28+
});
2629
});
2730
afterEach(function () {
2831
sinon.restore();
@@ -31,7 +34,7 @@ describe('class MongoDBResponse', () => {
3134
context('when enableUtf8Validation is not specified', () => {
3235
const options = { enableUtf8Validation: undefined };
3336
it('calls BSON deserialize with writeErrors validation turned off', () => {
34-
const res = new MongoDBResponse(BSON.serialize({}));
37+
const res = new MongoDBResponse(serialize({}));
3538
res.toObject(options);
3639

3740
expect(deseriailzeSpy).to.have.been.called;
@@ -49,7 +52,7 @@ describe('class MongoDBResponse', () => {
4952
context('when enableUtf8Validation is true', () => {
5053
const options = { enableUtf8Validation: true };
5154
it('calls BSON deserialize with writeErrors validation turned off', () => {
52-
const res = new MongoDBResponse(BSON.serialize({}));
55+
const res = new MongoDBResponse(serialize({}));
5356
res.toObject(options);
5457

5558
expect(deseriailzeSpy).to.have.been.called;
@@ -67,7 +70,7 @@ describe('class MongoDBResponse', () => {
6770
context('when enableUtf8Validation is false', () => {
6871
const options = { enableUtf8Validation: false };
6972
it('calls BSON deserialize with all validation disabled', () => {
70-
const res = new MongoDBResponse(BSON.serialize({}));
73+
const res = new MongoDBResponse(serialize({}));
7174
res.toObject(options);
7275

7376
expect(deseriailzeSpy).to.have.been.called;
@@ -87,7 +90,7 @@ describe('class MongoDBResponse', () => {
8790
describe('class CursorResponse', () => {
8891
describe('get cursor()', () => {
8992
it('throws if input does not contain cursor embedded document', () => {
90-
expect(() => new CursorResponse(BSON.serialize({ ok: 1 })).cursor).to.throw(
93+
expect(() => new CursorResponse(serialize({ ok: 1 })).cursor).to.throw(
9194
MongoUnexpectedServerResponseError,
9295
/"cursor" is missing/
9396
);
@@ -96,7 +99,7 @@ describe('class CursorResponse', () => {
9699

97100
describe('get id()', () => {
98101
it('throws if input does not contain cursor.id int64', () => {
99-
expect(() => new CursorResponse(BSON.serialize({ ok: 1, cursor: {} })).id).to.throw(
102+
expect(() => new CursorResponse(serialize({ ok: 1, cursor: {} })).id).to.throw(
100103
MongoUnexpectedServerResponseError,
101104
/"id" is missing/
102105
);
@@ -107,22 +110,22 @@ describe('class CursorResponse', () => {
107110
it('throws if input does not contain firstBatch nor nextBatch', () => {
108111
expect(
109112
// @ts-expect-error: testing private getter
110-
() => new CursorResponse(BSON.serialize({ ok: 1, cursor: { id: 0n, batch: [] } })).batch
113+
() => new CursorResponse(serialize({ ok: 1, cursor: { id: 0n, batch: [] } })).batch
111114
).to.throw(MongoUnexpectedServerResponseError, /did not contain a batch/);
112115
});
113116
});
114117

115118
describe('get ns()', () => {
116119
it('sets namespace to null if input does not contain cursor.ns', () => {
117-
expect(new CursorResponse(BSON.serialize({ ok: 1, cursor: { id: 0n, firstBatch: [] } })).ns)
118-
.to.be.null;
120+
expect(new CursorResponse(serialize({ ok: 1, cursor: { id: 0n, firstBatch: [] } })).ns).to.be
121+
.null;
119122
});
120123
});
121124

122125
describe('get batchSize()', () => {
123126
it('reports the returned batch size', () => {
124127
const response = new CursorResponse(
125-
BSON.serialize({ ok: 1, cursor: { id: 0n, nextBatch: [{}, {}, {}] } })
128+
serialize({ ok: 1, cursor: { id: 0n, nextBatch: [{}, {}, {}] } })
126129
);
127130
expect(response.batchSize).to.equal(3);
128131
expect(response.shift()).to.deep.equal({});
@@ -133,7 +136,7 @@ describe('class CursorResponse', () => {
133136
describe('get length()', () => {
134137
it('reports number of documents remaining in the batch', () => {
135138
const response = new CursorResponse(
136-
BSON.serialize({ ok: 1, cursor: { id: 0n, nextBatch: [{}, {}, {}] } })
139+
serialize({ ok: 1, cursor: { id: 0n, nextBatch: [{}, {}, {}] } })
137140
);
138141
expect(response).to.have.lengthOf(3);
139142
expect(response.shift()).to.deep.equal({});
@@ -146,7 +149,7 @@ describe('class CursorResponse', () => {
146149

147150
beforeEach(async function () {
148151
response = new CursorResponse(
149-
BSON.serialize({
152+
serialize({
150153
ok: 1,
151154
cursor: { id: 0n, nextBatch: [{ _id: 1 }, { _id: 2 }, { _id: 3 }] }
152155
})
@@ -173,7 +176,7 @@ describe('class CursorResponse', () => {
173176

174177
beforeEach(async function () {
175178
response = new CursorResponse(
176-
BSON.serialize({
179+
serialize({
177180
ok: 1,
178181
cursor: { id: 0n, nextBatch: [{ _id: 1 }, { _id: 2 }, { _id: 3 }] }
179182
})

0 commit comments

Comments
 (0)