Skip to content

Commit

Permalink
Change serializer errors to use error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
evaline-ju committed Oct 5, 2020
1 parent 8f5d6a9 commit e7214f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
18 changes: 14 additions & 4 deletions lib/nodejs/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
'use strict';

const {type} = require('../utils');
const {createInvalidArgumentTypeError} = require('../errors');
const {
createInvalidArgumentTypeError,
createInvalidArgumentValueError
} = require('../errors');
// this is not named `mocha:parallel:serializer` because it's noisy and it's
// helpful to be able to write `DEBUG=mocha:parallel*` and get everything else.
const debug = require('debug')('mocha:serializer');
Expand Down Expand Up @@ -131,7 +134,12 @@ class SerializableEvent {
*/
constructor(eventName, originalValue, originalError) {
if (!eventName) {
throw new Error('expected a non-empty `eventName` string argument');
throw createInvalidArgumentValueError(
'Empty `eventName` string argument',
'eventName',
eventName,
'is empty'
);
}
/**
* The event name.
Expand All @@ -140,8 +148,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'
);
}
/**
Expand Down
20 changes: 8 additions & 12 deletions test/node-unit/serializer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_VALUE'
});
});
});

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'
});
});
});
});
Expand Down

0 comments on commit e7214f5

Please sign in to comment.