Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda