TS Jest 无法区分自定义错误 class 和标准 javascript 错误 class

TS Jest not distinguishing custom error class from the standard javascript error class

我正在使用 TS Jest,并且我正在尝试测试函数是否抛出我创建的名为 InvalidOperatorError 的自定义错误。

它是使用 ES6 导入从另一个文件导入的,它被检测为错误而不是 InvalidOperatorError。

控制台记录的测试结果错误信息:

expect(received).toThrow(expected)

Expected constructor: InvalidOperatorError
Received constructor: Error

Received message: "Invalid operator!"

我的测试是这样写的:

it('throws InvalidOperatorError when the operator passed is \"NONE\"', () => {
    expect(() => {calculate(0, 0, Operator.NONE)}).toThrow(InvalidOperatorError)
})

我的自定义错误class(从单独的文件导入):

export class InvalidOperatorError extends Error {
    constructor(message: string | undefined){
        super(message)
        this.name = "InvalidOperatorError"
    }
}

计算函数:

export const calculate = (n1: number, n2: number, op: Operator): number => {
    switch(op){
        case SUM: return n1 + n2
        case SUB: return n1 - n2
        case MULT: return n1 * n2
        case DIV: return n1 / n2
        default: throw new InvalidOperatorError("Invalid operator!")
    }
}

枚举:

export enum Operator {
    NONE,
    SUM,
    SUB,
    MULT,
    DIV
}

终于发现我的问题出在 tsconfig 文件中,在编译器选项中,目标设置为 ES5 而不是 ES6。代码运行正常,问题出在TS的配置上。