Given a class and method such as:
export default class Router {
constructor(private request: Request) {
this.request = request;
}
public async handle(): Promise<string | undefined> {
const pathname = new URL(this.request.url).pathname;
if (pathname !== "/requests" && pathname !== "/shipments") {
throw new ValidatorError(
"ERR_HTTP_PATH",
"Not Found",
404,
);
...
}
And a unit test such as:
Deno.test("router should throw when endpoint is not /requests nor /shipments", async () => {
const fakeError = {
message: "ERR_HTTP_PATH",
cause: "Not Found",
status: 404,
};
const stub = sinon.stub(Router.prototype, "handle");
stub.rejects(fakeError);
const fakeRequest = new Request("http://fakerequest:5000/fakerequest");
const path = new URL(fakeRequest.url).pathname;
asserts.assertNotEquals(path, "/requests");
asserts.assertNotEquals(path, "/shipments");
await new Router(fakeRequest)
.handle()
.catch(err => asserts.assertEquals(err, fakeError))
.finally(() => {
asserts.assert(stub.called);
stub.restore();
});
});
This test pass. But why the test coverage reports it as not covered?
12 if (pathname !== "/requests" && pathname !== "/shipments") {
13 | throw new ValidatorError(
14 | "ERR_HTTP_PATH",
15 | "Not Found",
16 | 404,
17 | );
18 | }
I have checked the coverage is not cached internally and it is not. Docs actually says that --coverage is an accurate representation of the code coverage from the V8 engine.
What am I doing wrong here?
Do I have to necessarily stub out the ValidatorError class even though I have stubbed the handle method to throw and replace the class behavior?
What do I have to do to get this covered in the coverage report?
Thank you