Attempting to utilize the approach outlined in this SO post.
I have the following typed exception:
/**
* @param message The error message
* @param value The value that violates the constraint
* @param field The name of the field
* @param type The expected type for the field
* @param code The application or module code for the error
*/
export class IsError extends Error {
constructor(
public message:string,
public value: any,
public field:string,
public type: string,
public code?:string) {
super(message);
this.name = 'IsError';
}
}
This function throws it:
export function isBooleanError(value: any, field:string, code?: string): void {
if (!isBoolean(value)) {
const message:string = `The field ${field} should be a boolean valued. It is set to ${value}. `;
throw new IsError(message, value, field, BOOLEAN_TYPE, code);
}
}
This works:
expect(()=>{isBooleanError({}, '')}).toThrow(Error);
But this does not:
expect(()=>{isBooleanError({}, '')}).toThrow(IsError);
Anyone know why the latter does not work? Jest logs:
expect(received).toThrow(expected)
Expected name: "IsError"
Received name: "Error"