Skip to content

Commit

Permalink
test(NODE-4910): clean up removed BSONTypeError from tests (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Jan 4, 2023
1 parent e0dbb17 commit 9679ec3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
10 changes: 5 additions & 5 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,14 +1410,14 @@ describe('BSON', function () {

expect(() => {
parser.deserialize(data);
}).to.throw();
}).to.throw(BSONError);

data = Buffer.alloc(5);
data[0] = 0xff;
data[1] = 0xff;
expect(() => {
parser.deserialize(data);
}).to.throw();
}).to.throw(BSONError);

// Finish up
done();
Expand Down Expand Up @@ -1896,9 +1896,9 @@ describe('BSON', function () {
['c', badArray]
]);

expect(() => BSON.serialize(badDoc)).to.throw();
expect(() => BSON.serialize(badArray)).to.throw();
expect(() => BSON.serialize(badMap)).to.throw();
expect(() => BSON.serialize(badDoc)).to.throw(BSONError);
expect(() => BSON.serialize(badArray)).to.throw(BSONError);
expect(() => BSON.serialize(badMap)).to.throw(BSONError);
});

describe('Should support util.inspect for', function () {
Expand Down
5 changes: 3 additions & 2 deletions test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as BSON from '../register-bson';
const EJSON = BSON.EJSON;
import * as vm from 'node:vm';
import { expect } from 'chai';
import { BSONError } from '../../src';

// BSON types
const Binary = BSON.Binary;
Expand Down Expand Up @@ -511,8 +512,8 @@ describe('Extended JSON', function () {
const badDoc = { bad: badBsonType };
const badArray = [oid, badDoc];
// const badMap = new Map([['a', badBsonType], ['b', badDoc], ['c', badArray]]);
expect(() => EJSON.serialize(badDoc)).to.throw();
expect(() => EJSON.serialize(badArray)).to.throw();
expect(() => EJSON.serialize(badDoc)).to.throw(BSONError);
expect(() => EJSON.serialize(badArray)).to.throw(BSONError);
// expect(() => EJSON.serialize(badMap)).to.throw(); // uncomment when EJSON supports ES6 Map
});

Expand Down
33 changes: 15 additions & 18 deletions test/node/object_id_tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

const Buffer = require('buffer').Buffer;
const BSON = require('../register-bson');
const EJSON = BSON.EJSON;
const BSONTypeError = BSON.BSONTypeError;
const ObjectId = BSON.ObjectId;
const { BSON, BSONError, EJSON, ObjectId } = require('../register-bson');
const util = require('util');
const { expect } = require('chai');
const { bufferFromHexArray } = require('./tools/utils');
Expand Down Expand Up @@ -103,7 +100,7 @@ describe('ObjectId', function () {

for (const { input, description } of invalidInputs) {
it(`should throw error if ${description} is passed in`, function () {
expect(() => new ObjectId(input)).to.throw(BSONTypeError);
expect(() => new ObjectId(input)).to.throw(BSONError);
});
}

Expand All @@ -114,7 +111,7 @@ describe('ObjectId', function () {
return noArgObjID.toHexString();
}
};
expect(() => new ObjectId(objectIdLike)).to.throw(BSONTypeError);
expect(() => new ObjectId(objectIdLike)).to.throw(BSONError);
});

it('should correctly create ObjectId from object with valid string id', function () {
Expand Down Expand Up @@ -186,15 +183,15 @@ describe('ObjectId', function () {
const objectNullId = {
id: null
};
expect(() => new ObjectId(objectNumId)).to.throw(BSONTypeError);
expect(() => new ObjectId(objectNullId)).to.throw(BSONTypeError);
expect(() => new ObjectId(objectNumId)).to.throw(BSONError);
expect(() => new ObjectId(objectNullId)).to.throw(BSONError);
});

it('should throw an error if object with invalid string id is passed in', function () {
const objectInvalid24HexStr = {
id: 'FFFFFFFFFFFFFFFFFFFFFFFG'
};
expect(() => new ObjectId(objectInvalid24HexStr)).to.throw(BSONTypeError);
expect(() => new ObjectId(objectInvalid24HexStr)).to.throw(BSONError);
});

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

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

it('should throw error if non-12 byte non-24 hex string passed in', function () {
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFG')).to.throw(BSONTypeError);
expect(() => new ObjectId('thisstringisdefinitelytoolong')).to.throw(BSONTypeError);
expect(() => new ObjectId('tooshort')).to.throw(BSONTypeError);
expect(() => new ObjectId('101010')).to.throw(BSONTypeError);
expect(() => new ObjectId('')).to.throw(BSONTypeError);
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFG')).to.throw(BSONError);
expect(() => new ObjectId('thisstringisdefinitelytoolong')).to.throw(BSONError);
expect(() => new ObjectId('tooshort')).to.throw(BSONError);
expect(() => new ObjectId('101010')).to.throw(BSONError);
expect(() => new ObjectId('')).to.throw(BSONError);
});

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

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

it('should correctly allow for node.js inspect to work with ObjectId', function (done) {
Expand All @@ -345,11 +342,11 @@ describe('ObjectId', function () {
const characterCodesLargerThan256 = 'abcdefŽhijkl';
const length12Not12Bytes = '🐶🐶🐶🐶🐶🐶';
expect(() => new ObjectId(characterCodesLargerThan256).toHexString()).to.throw(
BSONTypeError,
BSONError,
'Argument passed in must be a string of 12 bytes'
);
expect(() => new ObjectId(length12Not12Bytes).id).to.throw(
BSONTypeError,
BSONError,
'Argument passed in must be a string of 12 bytes'
);
});
Expand Down
11 changes: 5 additions & 6 deletions test/node/uuid_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const { Buffer } = require('buffer');
const { Binary, UUID } = require('../register-bson');
const { inspect } = require('util');
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
const BSON = require('../register-bson');
const BSONTypeError = BSON.BSONTypeError;
const { BSON, BSONError } = require('../register-bson');
const BSON_DATA_BINARY = BSON.BSONType.binData;
const { BSON_BINARY_SUBTYPE_UUID_NEW } = require('../../src/constants');

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

Expand All @@ -89,7 +88,7 @@ describe('UUID', () => {
*/
it('should throw if passed unsupported argument', () => {
expect(() => new UUID(LOWERCASE_DASH_SEPARATED_UUID_STRING)).to.not.throw();
expect(() => new UUID({})).to.throw(BSONTypeError);
expect(() => new UUID({})).to.throw(BSONError);
});

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

expect(() => binRand.toUUID()).to.throw();
expect(() => binRand.toUUID()).to.throw(BSONError);

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

expect(() => binV3.toUUID()).to.throw();
expect(() => binV3.toUUID()).to.throw(BSONError);

const validUuidV4String = 'bd2d74fe-bad8-430c-aeac-b01d073a1eb6';
const validUuidV4Buffer = Buffer.from(validUuidV4String.replace(/-/g, ''), 'hex');
Expand Down
9 changes: 9 additions & 0 deletions test/register-bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ const { loadCJSModuleBSON } = require('./load_bson');
chai.use(function (chai) {
const throwsAssertion = chai.Assertion.prototype.throw;
chai.Assertion.addMethod('throw', function (...args) {
const isNegated = chai.util.flag(this, 'negate');
if (!isNegated) {
// to.not.throw() is acceptable to not have an argument
// to.throw() should always provide an Error class
expect(args).to.have.length.of.at.least(1);
// prevent to.throw(undefined) nor to.throw(null)
expect(args[0]).to.exist;
}

try {
throwsAssertion.call(this, ...args);
} catch (assertionError) {
Expand Down

0 comments on commit 9679ec3

Please sign in to comment.