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

154
Visualizações
What is Mocha test case for having caught a TypeError from an async function?

I have the following function in something.js, a browser script.

async function somethingFunction(value) {
  try {

//Lots of lovely code manipulating 'value', 
//an improper assignment here by passing 'undefined' 
//to value argument of function would cause a TypeError
//When trying to access property of value (cannot access .test of undefined)

    let somethingVar = value.test;

    return somethingVar;
    
  } catch (e) {
    throw new Error();
  }
}

And I've written as test as such:

describe(`#somethingFunction()`, async function () {
  context(`Passing undefined as value parameter should be caught be try catch block`, function () {
    it(`Should return error (TypeError)`, async function () {
      expect(async function () {
        await somethingFunction(undefined);
      }).to.throw(TypeError);
    });
  });
});

On this test, Mocha give:

AssertionError: expected [Function] to throw 'TypeError'

But passing undefined to this function does in fact cause a error - A TypeError!

Are TypeErrors caught? How would Mocha test this?

Tl;dr - How do I catch TypeErrors and get Mocha to test for them?

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