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

237
Visualizações
Jest not catching my thrown error in wrapped function

I expect my function to throw an error when it's missing an argument.

I understand that, when I'm trying to test if something is thrown from a function, I need to wrap it.

As such my test looks like this:

describe('FUNCTION: saveImageToS3', () => {
    let date = new Date("2022-04-06T06:30:59Z");

    test('throws error when missing required parameters', () => {
        expect(() => utils.saveImageToS3(undefined, date)).toThrow(`Missing required parameter for saveImageToS3:`)
    })
})

My function looks like this:

exports.saveImageToS3 = async (camId, captureDate ) => {  
    if (!camId || !captureDate) 
        throw new Error(`Missing required parameter for saveImageToS3: ${!camId ? 'camId':''} ${!captureDate ?'captureDate':''}`)
    }

    try {
    /// ... do stuff with s3....//

    } catch (err) {
        throw err;
    }
}

When I go to run my test, I get the following errror in the console:

D:\path\apis\lambdas\utils\utils.js:4561 throw new Error(`Missing required parameter for saveImageToS3: ${!camId ? ^

Error: Missing required parameter for saveImageToS3: camId at Object..exports.saveImageToS3 (D:path\apis\lambdas\utils\utils.js:81:9) at saveImageToS3 (D:\path\apis\lambdas\utils\utils.test.js:71:23) at Object. (D:\path\node_modules\expect\build\toThrowMatchers.js:83:11) at Object.throwingMatcher [as toThrow] (D:\path\node_modules\expect\build\index.js:342:21) at Object.toThrow (D:\path\apis\lambdas\utils\utils.test.js:71:80) at Promise.then.completed (D:\path\node_modules\jest-circus\build\utils.js:323:28) at new Promise () at callAsyncCircusFn (D:\path\node_modules\jest-circus\build\utils.js:249:10) at _callCircusTest (D:\path\node_modules\jest-circus\build\run.js:276:40) at _runTest (D:\path\node_modules\jest-circus\build\run.js:208:3)

... and thus the test runner crashes.

How can I get this test to run?

I will note, I have other 'toThrow' tests that seem to work perfectly fine that follow the same path here.

EDIT: of note, I've tried a named-function wrapping as well, with the same results of the test runner crashing:

const wrappedFunction = () => utils.saveImageToS3(undefined, date) 

    test('throws error when missing required parameters', () => {
        expect(wrappedFunction).toThrow(`Missing required parameter for saveImageToS3:`)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The tested function is an async function.

The test code isn't awaiting the function, so effectively, the error is thrown after Jest's attempt to catch it. Add an await to the function call.

Also need to add 'rejects' because you are looking at a failed promise. Like this:

test('throws error when missing required parameters', async () => {
    await expect(async () => await utils.saveImageToS3(undefined, date)).rejects.toThrow(`Missing required parameter for saveImageToS3:`)
})
about 4 years ago · Juan Pablo Isaza Relatório
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