Skip to content

Commit

Permalink
Merge pull request #19 from hckrnews/feature/type-async-function
Browse files Browse the repository at this point in the history
Test the error stack
  • Loading branch information
w3nl authored Dec 14, 2021
2 parents efdac24 + 625219a commit a894e49
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/error",
"description": "Extended Errors",
"version": "0.3.5",
"version": "0.3.6",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
81 changes: 81 additions & 0 deletions src/__tests__/app-error.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,85 @@ describe('App Error test', () => {
expect(error.stack.includes('AppError: Invalid error')).toEqual(true);
expect(error.me).toEqual(AppError);
});

it('It should catch the error from the stack', () => {
const DoTwo = () => {
throw new AppError({
value: 'test',
type: String,
message: 'Example from two',
me: AppError,
});
};

const DoOne = () => {
try {
DoTwo();
} catch (error) {
throw new AppError({
value: 'test',
type: String,
message: 'Example from one',
me: AppError,
});
}
};

expect(() => {
DoOne();
}).toThrowError('Example from one');

try {
DoOne();
} catch (error) {
expect(error instanceof AppError).toEqual(true);
expect(error instanceof Error).toEqual(true);
expect(error.name).toEqual('AppError');
expect(error.message).toEqual('Example from one');
expect(error.value).toEqual('test');
expect(error.status).toEqual(500);
expect(error.type).toEqual(String);
expect(error.date.constructor).toEqual(Date);
expect(error.stack.includes('AppError: Example from one')).toEqual(
true
);
expect(error.me).toEqual(AppError);
}
});

it('It should catch the error from the sub stack', () => {
const DoFour = () => {
throw new AppError({
value: 'test',
type: String,
message: 'Example from four',
me: AppError,
});
};

const DoThree = () => {
DoFour();
};

expect(() => {
DoThree();
}).toThrowError('Example from four');

try {
DoThree();
} catch (error) {
expect(error instanceof AppError).toEqual(true);
expect(error instanceof Error).toEqual(true);
expect(error.name).toEqual('AppError');
expect(error.message).toEqual('Example from four');
expect(error.value).toEqual('test');
expect(error.status).toEqual(500);
expect(error.type).toEqual(String);
expect(error.date.constructor).toEqual(Date);
expect(error.stack.includes('AppError: Example from four')).toEqual(
true
);
expect(error.me).toEqual(AppError);
}
});
});

0 comments on commit a894e49

Please sign in to comment.