Skip to content

Commit

Permalink
🛠️ add test cases when captureStackTrace is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
necojackarc committed Oct 7, 2018
1 parent d773b15 commit 7ed21aa
Showing 1 changed file with 64 additions and 44 deletions.
108 changes: 64 additions & 44 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,82 @@ describe('ExtensibleCustomError', function() {
describe('MyError extends ExtensibleCustomError', function() {
class MyError extends ExtensibleCustomError {}
const className = 'MyError';
const captureStackTrace = Error.captureStackTrace;

context('given a message', function() {
it('should contain name, message, and stack properly', function() {
const message = 'message';
const myError = new MyError(message);
[true, false].forEach((isCaptureStackTraceAvailable) => {
const availability =
isCaptureStackTraceAvailable ? 'available' : 'unavailable';

// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(`${className}: ${message}`);
context(`when captureStackTrace is ${availability}`, function() {
before(function() {
if (!isCaptureStackTraceAvailable) {
delete Error.captureStackTrace;
}
});

expect(myError.name).to.equal(className);
expect(myError.message).to.equal(message);
after(function() {
if (!isCaptureStackTraceAvailable) {
Error.captureStackTrace = captureStackTrace;
}
});

expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
});
});
context('given a message', function() {
it('should contain name, message, and stack properly', function() {
const message = 'message';
const myError = new MyError(message);

context('given an error', function() {
it('should contain name, message, and stack properly', function() {
const originalMessage = 'originalMessage';
const errorToWrap = new Error(originalMessage);
const myError = new MyError(errorToWrap);
// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(`${className}: ${message}`);

// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(
`${className}: ${errorToWrap.toString()}`
);
expect(myError.name).to.equal(className);
expect(myError.message).to.equal(message);

expect(myError.name).to.equal(className);
expect(myError.message).to.equal(errorToWrap.toString());
expect(myError.stack).to.include(errorToWrap.stack);
expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
});
});

expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
});
});
context('given an error', function() {
it('should contain name, message, and stack properly', function() {
const originalMessage = 'originalMessage';
const errorToWrap = new Error(originalMessage);
const myError = new MyError(errorToWrap);

// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(
`${className}: ${errorToWrap.toString()}`
);

expect(myError.name).to.equal(className);
expect(myError.message).to.equal(errorToWrap.toString());
expect(myError.stack).to.include(errorToWrap.stack);

expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
});
});

context('given a message and an error', function() {
it('should contain name, message, and stack properly', function() {
const originalMessage = 'originalMessage';
const errorToWrap = new Error(originalMessage);
const message = 'message';
const myError = new MyError(message, errorToWrap);
context('given a message and an error', function() {
it('should contain name, message, and stack properly', function() {
const originalMessage = 'originalMessage';
const errorToWrap = new Error(originalMessage);
const message = 'message';
const myError = new MyError(message, errorToWrap);

// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(`${className}: ${message}`);
// toString() returns the first line of the stack trace
expect(myError.toString()).to.equal(`${className}: ${message}`);

expect(myError.name).to.equal(className);
expect(myError.message).to.equal(message);
expect(myError.stack).to.include(errorToWrap.stack);
expect(myError.name).to.equal(className);
expect(myError.message).to.equal(message);
expect(myError.stack).to.include(errorToWrap.stack);

expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
expect(myError).to.be.an.instanceof(Error);
expect(myError).to.be.an.instanceof(ExtensibleCustomError);
expect(myError).to.be.an.instanceof(MyError);
});
});
});
});
});
Expand Down

0 comments on commit 7ed21aa

Please sign in to comment.