lowcrawler
lowcrawler

Reputation: 7549

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:`)

Upvotes: 1

Views: 3513

Answers (2)

Ikram Ud Daula
Ikram Ud Daula

Reputation: 1321

Jest version: 29
You have an async function that needs to be tested for throwing an error.

await expect(utils.saveImageToS3(undefined, date)).rejects.toThrow();

Assert with the exact error message:

await expect(utils.saveImageToS3(undefined, date)).rejects.toThrow("Exact error message");

Upvotes: 0

lowcrawler
lowcrawler

Reputation: 7549

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:`)
})

Upvotes: 6

Related Questions