Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
♻️ change custom error handling to custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Feb 26, 2018
1 parent f05260f commit 285c7d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
22 changes: 12 additions & 10 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Assertion.addProperty('hexString', function handleAssert() {
const expected = Buffer.from(actual, 'hex').toString('hex');
this.assert(
expected === actual,
'expected #{this} to be a hexString',
'expected #{this} not to be a hexString',
'expected #{this} to be a hex string',
'expected #{this} not to be a hex string',
);
});

Expand All @@ -57,23 +57,25 @@ Assertion.addMethod('matchAny', function handleAssert(matcher) {
const obj = this._obj;

new Assertion(obj).to.be.an('array');
let result = false;
obj.forEach(val => {
if (matcher(val)) {
result = true;
}
});
const result = obj.some(val => matcher(val));
this.assert(
result,
'expected #{this} to match at least once',
'expected #{this} to not to match',
'expected #{this} not to match',
);
});

Assertion.addMethod('customError', function handleAssert(error) {
const obj = this._obj;
new Assertion(obj).to.be.instanceOf(Error);
new Assertion(obj.name).to.equal(error.name);
new Assertion(obj.message).to.equal(error.message);
});
/* eslint-enable no-underscore-dangle */

mochaBDD();

[sinonChai, chaiAsPromised].forEach(plugin => chai.use(plugin));
[sinonChai, chaiAsPromised].forEach(chai.use);

global.sinon = sinon;
global.sandbox = sinon.sandbox.create();
25 changes: 5 additions & 20 deletions test/steps/general/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
*/
import { getFirstQuotedString, getFirstNumber } from '../utils';
import { ValidationError, FileSystemError } from '../../../src/utils/error';

export function theErrorShouldBeInstanceOfNodesBuiltInError() {
const { testError } = this.test.ctx;
Expand All @@ -34,29 +35,17 @@ export function itShouldReturnTheResult() {
export function itShouldThrowValidationError() {
const { testFunction } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
testFunction.should.throw().and.is.instanceOf(Error);
testFunction.should
.throw()
.and.has.property('name')
.which.equal('ValidationError');
return testFunction.should
.throw()
.and.has.property('message')
.which.include(message);
.and.be.customError(new ValidationError(message));
}

export function itShouldThrowFileSystemError() {
const { testFunction } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
testFunction.should.throw().and.is.instanceOf(Error);
testFunction.should
.throw()
.and.has.property('name')
.which.equal('FileSystemError');
return testFunction.should
.throw()
.and.has.property('message')
.which.include(message);
.and.be.customError(new FileSystemError(message));
}

export function itShouldExitWithCode() {
Expand Down Expand Up @@ -98,19 +87,15 @@ export function itShouldRejectWithFileSystemErrorAndMessage() {
const { returnValue } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return returnValue.should.be.rejected.then(err => {
err.should.be.instanceOf(Error);
err.should.have.property('name').which.equal('FileSystemError');
return err.should.have.property('message').which.include(message);
return err.should.be.customError(new FileSystemError(message));
});
}

export function itShouldRejectWithValidationErrorAndMessage() {
const { returnValue } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return returnValue.should.be.rejected.then(err => {
err.should.be.instanceOf(Error);
err.should.have.property('name').which.equal('ValidationError');
return err.should.have.property('message').which.include(message);
return err.should.be.customError(new ValidationError(message));
});
}

Expand Down

0 comments on commit 285c7d1

Please sign in to comment.