玩笑单元测试是否调用了super() [英] Jest unit-testing if super() is called
问题描述
我有一个自定义错误类,该类扩展了Javascript中的内置Error类.我想到的问题是,是否通过我的Jest单元测试调用了"super()"方法.
I have a custom error class that extends the built-in Error class in Javascript. The problem I came up with is that "super()" method is not checked if it is called or not through my Jest unit testing.
export class AppError extends Error {
public name: string;
public message: string;
public status?: number;
public data?: any;
constructor(message: string, status?: number, data?: any) {
super(); <-- this guy!!
this.name = 'AppError';
this.status = status || 500;
this.message = message;
this.data = data;
}
}
有什么方法可以测试吗?谢谢.
Is there any way to test it? Thanks.
推荐答案
没有理由检查在本地ES6类或通过Babel转译的类中均未调用super()
.
There's no reason to check if super()
is called neither in native ES6 classes nor in classes transpiled with Babel.
在子类构造函数中不调用super
会导致类实例化时出错:
Not calling super
in child class constructor will result in error on class instantiation:
ReferenceError:必须在派生类中调用超级构造函数,然后才能访问"this"或从派生构造函数返回
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor