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.
There's no reason to check if
super()
is called neither in native ES6 classes nor in classes transpiled with Babel.Not calling
super
in child class constructor will result in error on class instantiation:Babel provides a safeguard for that as well:
It may be possible to check that parent constructor is called (could be useful to assert
super()
arguments) by mocking child class prototype, something like:It's expected to mock
super
in both native classes and classes transpiled with Babel.But this test is redundant, because missing
super()
will result in error any way. Testing thatAppError
inherits fromError
is everything that needs be tested here: