Skip to content

Commit ad2c4a5

Browse files
author
Grace Chong
committed
refactor: remove assertion override and add tests to check for correct error message and names
1 parent 098ea7f commit ad2c4a5

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

test/node/error_test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const BSONTypeError = BSON.BSONTypeError;
5+
const BSONError = BSON.BSONError;
6+
7+
describe('BSON error tests', function () {
8+
it('should correctly validate instanceof checks for BSONTypeError and BSONError', function () {
9+
const bsonTypeErr = new BSONTypeError();
10+
const bsonErr = new BSONError();
11+
12+
expect(bsonTypeErr instanceof BSONTypeError).to.be.true;
13+
expect(bsonErr instanceof BSONError).to.be.true;
14+
});
15+
16+
it('should correctly get correct names for BSONTypeError and BSONError', function () {
17+
const bsonTypeErr = new BSONTypeError('This is a BSONTypeError message');
18+
const bsonErr = new BSONError('This is a BSONError message');
19+
20+
expect(bsonTypeErr.name).equals('BSONTypeError');
21+
expect(bsonTypeErr.message).equals('This is a BSONTypeError message');
22+
expect(bsonErr.name).equals('BSONError');
23+
expect(bsonErr.message).equals('This is a BSONError message');
24+
});
25+
});

test/register-bson.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,4 @@ require('object.entries/auto');
1414
const BSON = require('../lib/bson');
1515
const { ensureBuffer } = require('../lib/ensure_buffer');
1616
BSON.ensureBuffer = ensureBuffer;
17-
18-
const { Assertion, util } = require('chai');
19-
Assertion.overwriteMethod('throw', function (original) {
20-
return function assertThrow(...args) {
21-
if (args.length === 0 || args.includes(BSON.BSONError) || args.includes(BSON.BSONTypeError)) {
22-
// By default, lets check for BSONError or BSONTypeError
23-
// Since we compile to es5 instanceof is broken???
24-
const assertion = original.apply(this, args);
25-
const object = util.flag(assertion, 'object');
26-
return this.assert(
27-
object && /BSONError|BSONTypeError/.test(object.stack),
28-
'expected #{this} to be a BSONError or a BSONTypeError but got #{act}',
29-
'expected #{this} to not be a BSONError nor a BSONTypeError but got #{act}',
30-
(object && object.stack) || '<error undefined>'
31-
);
32-
} else {
33-
return original.apply(this, args);
34-
}
35-
};
36-
});
37-
3817
module.exports = BSON;

0 commit comments

Comments
 (0)