Skip to content

Instantly share code, notes, and snippets.

@anandnimkar
Created June 29, 2018 03:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandnimkar/1c45c453a7edb9df0b3eb6d2a1cb9fa7 to your computer and use it in GitHub Desktop.
Save anandnimkar/1c45c453a7edb9df0b3eb6d2a1cb9fa7 to your computer and use it in GitHub Desktop.
toEqual does not deep equal check custom error objects
export class EError extends Error {
type: string;
data: { [key: string]: any } | undefined;
constructor(message: string, type: string, data?: { [key: string]: any }) {
super(message);
this.type = type;
this.data = data;
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}
}
}
export function createEError(message: string, type: string, data?: { [key: string]: any }): EError {
return new EError(message, type, data);
}
test("test error", async () => {
const error1 = createEError("test", "test", {number: 2});
const error2 = createEError("test", "test", {number: 3});
const fn = async () => {
throw error1;
};
expect(error1).toEqual(error2); // this should fail, but it passes
await expect(fn()).rejects.toEqual(error2); // this should fail, but it passes
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment