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

161
Vistas
How to test error handling functions in Javascript

I m trying to write unit test cases to handle error codes that are defined below. not sure how to achieve it.

module.exports = {
    Error: Err,
    BadRequest: function () {
        return new Err(400, 'Bad Request');
    },
    NotAcceptable: function () {
        return new Err(406, 'Not Acceptable');
    },
    NotAuthorized: function () {
        return new Err(401, 'Not Authorized');
    }
};

function Err(code, message) {
    assert(code, 'code');

    Error.call(this);
    Error.captureStackTrace(this, this.constructor);

    Object.defineProperty(this, 'code', {
        enumerable: true,
        value: code
    });

    this.reason = message;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should have a better base error layer by extending the Error class which already provide most of the functions you would need (capturing the stack trace automatically):

class Err extends Error {
    constructor(code, message) {
        super(message);
        this.code = code;
        this.reason = message;
    }
}

You can then performa your assertion using the throw expectation:

const errorFn = () => { throw new Err(401, 'Unauthorized') };

expect(errorFn).to.throw(Err).to.haveOwnProperty('code', 401).to.haveOwnProperty('reason', 'Unauthorized');

You should obviously update your sub-error accordingly to extend your generic error.

about 4 years ago · Juan Pablo Isaza Denunciar
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