From 9679ec3d98a986dcdf68afe625659d407044c547 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Wed, 4 Jan 2023 16:52:08 -0500 Subject: [PATCH] test(NODE-4910): clean up removed BSONTypeError from tests (#546) --- test/node/bson_test.js | 10 +++++----- test/node/extended_json.test.ts | 5 +++-- test/node/object_id_tests.js | 33 +++++++++++++++------------------ test/node/uuid_tests.js | 11 +++++------ test/register-bson.js | 9 +++++++++ 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/test/node/bson_test.js b/test/node/bson_test.js index b71c4d6e..ee2c5b9a 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -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(); @@ -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 () { diff --git a/test/node/extended_json.test.ts b/test/node/extended_json.test.ts index 4a372088..3e25c559 100644 --- a/test/node/extended_json.test.ts +++ b/test/node/extended_json.test.ts @@ -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; @@ -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 }); diff --git a/test/node/object_id_tests.js b/test/node/object_id_tests.js index 0d3e37b3..8bbf4420 100644 --- a/test/node/object_id_tests.js +++ b/test/node/object_id_tests.js @@ -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'); @@ -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); }); } @@ -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 () { @@ -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 () { @@ -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 () { @@ -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 () { @@ -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) { @@ -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' ); }); diff --git a/test/node/uuid_tests.js b/test/node/uuid_tests.js index 9224f7b6..f5088c4a 100644 --- a/test/node/uuid_tests.js +++ b/test/node/uuid_tests.js @@ -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'); @@ -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 ^ }); @@ -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); }); /** @@ -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'); diff --git a/test/register-bson.js b/test/register-bson.js index 3dd57b58..37fda7e0 100644 --- a/test/register-bson.js +++ b/test/register-bson.js @@ -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) {