Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Sinon unit test pass as expected but coverage report shows it is not covered

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

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda