【发布时间】:2019-07-27 15:33:06
【问题描述】:
Attempting to utilize the approach outlined in this SO post.
我有以下类型的异常:
/**
* @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';
}
}
这个函数抛出它:
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);
}
}
这行得通:
expect(()=>{isBooleanError({}, '')}).toThrow(Error);
但这不是:
expect(()=>{isBooleanError({}, '')}).toThrow(IsError);
有人知道为什么后者不起作用吗?笑话日志:
expect(received).toThrow(expected)
Expected name: "IsError"
Received name: "Error"
【问题讨论】:
标签: javascript typescript unit-testing jestjs