Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Change serializer errors to use error codes (#4464)

Evaline Jufd9fe95e2c8f
diff --git a/lib/nodejs/serializer.js b/lib/nodejs/serializer.js
index 448cb52e..70640bc5 100644
--- a/lib/nodejs/serializer.js
+++ b/lib/nodejs/serializer.js
@@ -131,7 +131,11 @@ class SerializableEvent {
*/
constructor(eventName, originalValue, originalError) {
if (!eventName) {
- throw new Error('expected a non-empty `eventName` string argument');
+ throw createInvalidArgumentTypeError(
+ 'Empty `eventName` string argument',
+ 'eventName',
+ 'string'
+ );
}
/**
* The event name.
@@ -140,8 +144,10 @@ class SerializableEvent {
this.eventName = eventName;
const originalValueType = type(originalValue);
if (originalValueType !== 'object' && originalValueType !== 'undefined') {
- throw new Error(
- `expected object, received [${originalValueType}]: ${originalValue}`
+ throw createInvalidArgumentTypeError(
+ `Expected object but received ${originalValueType}`,
+ 'originalValue',
+ 'object'
);
}
/**
diff --git a/test/node-unit/serializer.spec.js b/test/node-unit/serializer.spec.js
index 86848d90..0169338a 100644
--- a/test/node-unit/serializer.spec.js
+++ b/test/node-unit/serializer.spec.js
@@ -88,22 +88,18 @@ describe('serializer', function() {
describe('SerializableEvent', function() {
describe('constructor', function() {
describe('when called without `eventName`', function() {
- it('should throw', function() {
- expect(
- () => new SerializableEvent(),
- 'to throw',
- /expected a non-empty `eventName`/
- );
+ it('should throw "invalid arg value" error', function() {
+ expect(() => new SerializableEvent(), 'to throw', {
+ code: 'ERR_MOCHA_INVALID_ARG_TYPE'
+ });
});
});
describe('when called with a non-object `rawObject`', function() {
- it('should throw', function() {
- expect(
- () => new SerializableEvent('blub', 'glug'),
- 'to throw',
- /expected object, received \[string\]/
- );
+ it('should throw "invalid arg type" error', function() {
+ expect(() => new SerializableEvent('blub', 'glug'), 'to throw', {
+ code: 'ERR_MOCHA_INVALID_ARG_TYPE'
+ });
});
});
});