Skip to content

Commit 5d01f60

Browse files
committed
Fix tests
1 parent e3bf7de commit 5d01f60

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

test/node/bson_test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ const MaxKey = BSON.MaxKey;
2020
const BSONError = BSON.BSONError;
2121
const { BinaryParser } = require('./tools/binary_parser');
2222
const vm = require('vm');
23-
const { assertBuffersEqual, isBufferOrUint8Array } = require('./tools/utils');
23+
const {
24+
assertBuffersEqual,
25+
isBufferOrUint8Array,
26+
assertDeepEqualsWithObjectId
27+
} = require('./tools/utils');
2428
const { inspect } = require('util');
2529

2630
/**
@@ -707,7 +711,7 @@ describe('BSON', function () {
707711
expect(serialized_data).to.deep.equal(serialized_data2);
708712

709713
var doc2 = b.deserialize(serialized_data);
710-
expect(doc).to.deep.equal(doc2);
714+
assertDeepEqualsWithObjectId(doc, doc2);
711715
expect(doc2.dbref.oid.toHexString()).to.deep.equal(oid.toHexString());
712716
done();
713717
});
@@ -1001,7 +1005,7 @@ describe('BSON', function () {
10011005

10021006
var deserialized_data = BSON.deserialize(serialized_data);
10031007
expect(doc.b).to.deep.equal(deserialized_data.b);
1004-
expect(doc).to.deep.equal(deserialized_data);
1008+
assertDeepEqualsWithObjectId(doc, deserialized_data);
10051009
done();
10061010
});
10071011

@@ -1213,7 +1217,7 @@ describe('BSON', function () {
12131217

12141218
var doc2 = BSON.deserialize(serialized_data);
12151219

1216-
expect(doc).to.deep.equal(doc2);
1220+
assertDeepEqualsWithObjectId(doc, doc2);
12171221
done();
12181222
});
12191223

test/node/bson_type_classes.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
UUID,
1919
BSONValue
2020
} from '../register-bson';
21+
import { assertDeepEqualsWithObjectId } from './tools/utils';
2122
import * as vm from 'node:vm';
2223

2324
const BSONTypeClasses = [
@@ -128,7 +129,13 @@ describe('BSON Type classes common interfaces', () => {
128129
ctx.ObjectId = ObjectId;
129130
}
130131
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
131-
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
132+
133+
if (ctx.ObjectId) {
134+
// Since ObjectId uses a pool we expect offset to be different
135+
assertDeepEqualsWithObjectId(ctx.module.exports.result, bsonValue);
136+
} else {
137+
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
138+
}
132139
});
133140
}
134141
});

test/node/extended_json.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vm from 'node:vm';
44
import { expect } from 'chai';
55
import { BSONVersionError, BSONRuntimeError } from '../../src';
66
import { BSONError } from '../register-bson';
7+
import { assertDeepEqualsWithObjectId } from './tools/utils';
78

89
// BSON types
910
const Binary = BSON.Binary;
@@ -144,9 +145,10 @@ describe('Extended JSON', function () {
144145
const input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}';
145146
const parsed = EJSON.parse(input);
146147

147-
expect(parsed).to.deep.equal({
148+
const expected = {
148149
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
149-
});
150+
};
151+
assertDeepEqualsWithObjectId(parsed, expected);
150152
});
151153

152154
it('should correctly throw when passed a non-string to parse', function () {

test/node/object_id.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { expect } from 'chai';
55
import { bufferFromHexArray } from './tools/utils';
66
import { isBufferOrUint8Array } from './tools/utils';
77

8-
ObjectId.poolSize = 100;
9-
108
declare module '../register-bson' {
119
interface ObjectId {
1210
pool: Uint8Array;

test/node/tools/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,25 @@ module.exports.sorted = (iterable, how) => {
218218
items.sort(how);
219219
return items;
220220
};
221+
222+
/** Deeply converts all ObjectIds into their hex value representation */
223+
const deepOidValue = obj => {
224+
if (obj?._bsontype === 'ObjectId') {
225+
return obj.toHexString();
226+
}
227+
if (Array.isArray(obj)) {
228+
return obj.map(item => deepOidValue(item));
229+
} else if (obj !== null && typeof obj === 'object' && !Buffer.isBuffer(obj)) {
230+
return Object.entries(obj).reduce((acc, [key, value]) => {
231+
acc[key] = deepOidValue(value);
232+
return acc;
233+
}, {});
234+
}
235+
return obj;
236+
};
237+
238+
module.exports.assertDeepEqualsWithObjectId = (actual, expected) => {
239+
const a = deepOidValue(actual);
240+
const b = deepOidValue(expected);
241+
expect(a).to.deep.equal(b);
242+
};

0 commit comments

Comments
 (0)