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

246
Vistas
How to assert that function throws with a specific error message

I'm using Node v18 ( with the built-in testrunner) and the package assert/strict to test that a function call throws an error with a custom error message.

I think my code should look like

assert.throws(() => myFunction(), 'content of error.message goes here');

Unfortunately I get the following error

error: 'The "error/message" argument is ambiguous. The error message "content of error.message goes here" is identical to the message.'

enter image description here

I also tried

assert.throws(
  () => myFunction(), 
  error => {
    assert.ok(error instanceof CustomError);
    assert.strictEqual(error.message, 'content of error.message goes here');

    return true;
  });

and

assert.throws(myFunction, 'content of error.message goes here');

but unfortunately that didn't help. This might be a duplicate of node assert: Test error message but I don't want to pass in regular expressions because there is no need for it.

Does someone know how to fix the assertion?

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