Jest is not covering super call in my custom error class

Viewed 173

Well, I just created a custom error class, which extends the Error class, and I'm not directly testing that class, but it is being covered when I test other files that call my custom error. However, in my constructor I had to call super(message) from Error, but it is not being covered by jest.

This is my error class code:

export class ApiError extends Error {
    constructor(public statusCode: number, message: string) {
        super(message);
        this.name = 'ApiError';
        this.statusCode = statusCode;
        this.message = message;

        Object.setPrototypeOf(this, ApiError.prototype);
    }

    static badRequest(message: string): ApiError {
        return new ApiError(400, message);
    }

    static unauthorized(message: string): ApiError {
        return new ApiError(401, message);
    }

    static forbidden(message: string): ApiError {
        return new ApiError(403, message);
    }
}

And this is the coverage report: coverage report

Just consider MongooseError.ts really similar to ApiError.ts, both are extending Error and calling super(message).

What should I do, since I'm not calling any method from super class?

PS.: I don't know what other code files I need to share, since the class is being tested on both controllers and also in some services.

0 Answers
Related