Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!