Created
June 29, 2018 03:42
-
-
Save anandnimkar/1c45c453a7edb9df0b3eb6d2a1cb9fa7 to your computer and use it in GitHub Desktop.
toEqual does not deep equal check custom error objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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