Skip to content

Commit 9679ec3

Browse files
authored
test(NODE-4910): clean up removed BSONTypeError from tests (#546)
1 parent e0dbb17 commit 9679ec3

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

test/node/bson_test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,14 +1410,14 @@ describe('BSON', function () {
14101410

14111411
expect(() => {
14121412
parser.deserialize(data);
1413-
}).to.throw();
1413+
}).to.throw(BSONError);
14141414

14151415
data = Buffer.alloc(5);
14161416
data[0] = 0xff;
14171417
data[1] = 0xff;
14181418
expect(() => {
14191419
parser.deserialize(data);
1420-
}).to.throw();
1420+
}).to.throw(BSONError);
14211421

14221422
// Finish up
14231423
done();
@@ -1896,9 +1896,9 @@ describe('BSON', function () {
18961896
['c', badArray]
18971897
]);
18981898

1899-
expect(() => BSON.serialize(badDoc)).to.throw();
1900-
expect(() => BSON.serialize(badArray)).to.throw();
1901-
expect(() => BSON.serialize(badMap)).to.throw();
1899+
expect(() => BSON.serialize(badDoc)).to.throw(BSONError);
1900+
expect(() => BSON.serialize(badArray)).to.throw(BSONError);
1901+
expect(() => BSON.serialize(badMap)).to.throw(BSONError);
19021902
});
19031903

19041904
describe('Should support util.inspect for', function () {

test/node/extended_json.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as BSON from '../register-bson';
22
const EJSON = BSON.EJSON;
33
import * as vm from 'node:vm';
44
import { expect } from 'chai';
5+
import { BSONError } from '../../src';
56

67
// BSON types
78
const Binary = BSON.Binary;
@@ -511,8 +512,8 @@ describe('Extended JSON', function () {
511512
const badDoc = { bad: badBsonType };
512513
const badArray = [oid, badDoc];
513514
// const badMap = new Map([['a', badBsonType], ['b', badDoc], ['c', badArray]]);
514-
expect(() => EJSON.serialize(badDoc)).to.throw();
515-
expect(() => EJSON.serialize(badArray)).to.throw();
515+
expect(() => EJSON.serialize(badDoc)).to.throw(BSONError);
516+
expect(() => EJSON.serialize(badArray)).to.throw(BSONError);
516517
// expect(() => EJSON.serialize(badMap)).to.throw(); // uncomment when EJSON supports ES6 Map
517518
});
518519

test/node/object_id_tests.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
'use strict';
22

33
const Buffer = require('buffer').Buffer;
4-
const BSON = require('../register-bson');
5-
const EJSON = BSON.EJSON;
6-
const BSONTypeError = BSON.BSONTypeError;
7-
const ObjectId = BSON.ObjectId;
4+
const { BSON, BSONError, EJSON, ObjectId } = require('../register-bson');
85
const util = require('util');
96
const { expect } = require('chai');
107
const { bufferFromHexArray } = require('./tools/utils');
@@ -103,7 +100,7 @@ describe('ObjectId', function () {
103100

104101
for (const { input, description } of invalidInputs) {
105102
it(`should throw error if ${description} is passed in`, function () {
106-
expect(() => new ObjectId(input)).to.throw(BSONTypeError);
103+
expect(() => new ObjectId(input)).to.throw(BSONError);
107104
});
108105
}
109106

@@ -114,7 +111,7 @@ describe('ObjectId', function () {
114111
return noArgObjID.toHexString();
115112
}
116113
};
117-
expect(() => new ObjectId(objectIdLike)).to.throw(BSONTypeError);
114+
expect(() => new ObjectId(objectIdLike)).to.throw(BSONError);
118115
});
119116

120117
it('should correctly create ObjectId from object with valid string id', function () {
@@ -186,15 +183,15 @@ describe('ObjectId', function () {
186183
const objectNullId = {
187184
id: null
188185
};
189-
expect(() => new ObjectId(objectNumId)).to.throw(BSONTypeError);
190-
expect(() => new ObjectId(objectNullId)).to.throw(BSONTypeError);
186+
expect(() => new ObjectId(objectNumId)).to.throw(BSONError);
187+
expect(() => new ObjectId(objectNullId)).to.throw(BSONError);
191188
});
192189

193190
it('should throw an error if object with invalid string id is passed in', function () {
194191
const objectInvalid24HexStr = {
195192
id: 'FFFFFFFFFFFFFFFFFFFFFFFG'
196193
};
197-
expect(() => new ObjectId(objectInvalid24HexStr)).to.throw(BSONTypeError);
194+
expect(() => new ObjectId(objectInvalid24HexStr)).to.throw(BSONError);
198195
});
199196

200197
it('should correctly create ObjectId from object with invalid string id and toHexString method', function () {
@@ -213,7 +210,7 @@ describe('ObjectId', function () {
213210
const objectInvalidBuffer = {
214211
id: Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
215212
};
216-
expect(() => new ObjectId(objectInvalidBuffer)).to.throw(BSONTypeError);
213+
expect(() => new ObjectId(objectInvalidBuffer)).to.throw(BSONError);
217214
});
218215

219216
it('should correctly create ObjectId from object with invalid Buffer id and toHexString method', function () {
@@ -270,11 +267,11 @@ describe('ObjectId', function () {
270267
});
271268

272269
it('should throw error if non-12 byte non-24 hex string passed in', function () {
273-
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFG')).to.throw(BSONTypeError);
274-
expect(() => new ObjectId('thisstringisdefinitelytoolong')).to.throw(BSONTypeError);
275-
expect(() => new ObjectId('tooshort')).to.throw(BSONTypeError);
276-
expect(() => new ObjectId('101010')).to.throw(BSONTypeError);
277-
expect(() => new ObjectId('')).to.throw(BSONTypeError);
270+
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFG')).to.throw(BSONError);
271+
expect(() => new ObjectId('thisstringisdefinitelytoolong')).to.throw(BSONError);
272+
expect(() => new ObjectId('tooshort')).to.throw(BSONError);
273+
expect(() => new ObjectId('101010')).to.throw(BSONError);
274+
expect(() => new ObjectId('')).to.throw(BSONError);
278275
});
279276

280277
it('should correctly create ObjectId from 24 hex string', function () {
@@ -319,7 +316,7 @@ describe('ObjectId', function () {
319316

320317
it('should throw an error if invalid Buffer passed in', function () {
321318
const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
322-
expect(() => new ObjectId(a)).to.throw(BSONTypeError);
319+
expect(() => new ObjectId(a)).to.throw(BSONError);
323320
});
324321

325322
it('should correctly allow for node.js inspect to work with ObjectId', function (done) {
@@ -345,11 +342,11 @@ describe('ObjectId', function () {
345342
const characterCodesLargerThan256 = 'abcdefŽhijkl';
346343
const length12Not12Bytes = '🐶🐶🐶🐶🐶🐶';
347344
expect(() => new ObjectId(characterCodesLargerThan256).toHexString()).to.throw(
348-
BSONTypeError,
345+
BSONError,
349346
'Argument passed in must be a string of 12 bytes'
350347
);
351348
expect(() => new ObjectId(length12Not12Bytes).id).to.throw(
352-
BSONTypeError,
349+
BSONError,
353350
'Argument passed in must be a string of 12 bytes'
354351
);
355352
});

test/node/uuid_tests.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const { Buffer } = require('buffer');
44
const { Binary, UUID } = require('../register-bson');
55
const { inspect } = require('util');
66
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
7-
const BSON = require('../register-bson');
8-
const BSONTypeError = BSON.BSONTypeError;
7+
const { BSON, BSONError } = require('../register-bson');
98
const BSON_DATA_BINARY = BSON.BSONType.binData;
109
const { BSON_BINARY_SUBTYPE_UUID_NEW } = require('../../src/constants');
1110

@@ -80,7 +79,7 @@ describe('UUID', () => {
8079
*/
8180
it('should throw if passed invalid 36-char uuid hex string', () => {
8281
expect(() => new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.not.throw();
83-
expect(() => new UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')).to.throw(BSONTypeError);
82+
expect(() => new UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')).to.throw(BSONError);
8483
// Note: The version is missing here ^
8584
});
8685

@@ -89,7 +88,7 @@ describe('UUID', () => {
8988
*/
9089
it('should throw if passed unsupported argument', () => {
9190
expect(() => new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.not.throw();
92-
expect(() => new UUID({})).to.throw(BSONTypeError);
91+
expect(() => new UUID({})).to.throw(BSONError);
9392
});
9493

9594
/**
@@ -142,13 +141,13 @@ describe('UUID', () => {
142141
const validRandomBuffer = Buffer.from('Hello World!');
143142
const binRand = new Binary(validRandomBuffer);
144143

145-
expect(() => binRand.toUUID()).to.throw();
144+
expect(() => binRand.toUUID()).to.throw(BSONError);
146145

147146
const validUuidV3String = '25f0d698-15b9-3a7a-96b1-a573061e29c9';
148147
const validUuidV3Buffer = Buffer.from(validUuidV3String.replace(/-/g, ''), 'hex');
149148
const binV3 = new Binary(validUuidV3Buffer, Binary.SUBTYPE_UUID_OLD);
150149

151-
expect(() => binV3.toUUID()).to.throw();
150+
expect(() => binV3.toUUID()).to.throw(BSONError);
152151

153152
const validUuidV4String = 'bd2d74fe-bad8-430c-aeac-b01d073a1eb6';
154153
const validUuidV4Buffer = Buffer.from(validUuidV4String.replace(/-/g, ''), 'hex');

test/register-bson.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const { loadCJSModuleBSON } = require('./load_bson');
1919
chai.use(function (chai) {
2020
const throwsAssertion = chai.Assertion.prototype.throw;
2121
chai.Assertion.addMethod('throw', function (...args) {
22+
const isNegated = chai.util.flag(this, 'negate');
23+
if (!isNegated) {
24+
// to.not.throw() is acceptable to not have an argument
25+
// to.throw() should always provide an Error class
26+
expect(args).to.have.length.of.at.least(1);
27+
// prevent to.throw(undefined) nor to.throw(null)
28+
expect(args[0]).to.exist;
29+
}
30+
2231
try {
2332
throwsAssertion.call(this, ...args);
2433
} catch (assertionError) {

0 commit comments

Comments
 (0)