1

The code I'm trying to test:

const utils = require('../utils/utils');

        let imageBuffer;
        try {
            imageBuffer = await utils.retrieveImageFromURI(params)
            console.log(imageBuffer) // comes back as undefined when I mock the utils.retreieveImageFromURI
            if (!imageBuffer || imageBuffer.length < 1024) {
                throw new Error(`Retrieve from uri (${params.camera.ingest.uri}) was less than 1kb in size - indicating an error`)
            }
            console.log(`${params.camera.camId} - Successful Ingestion from URI`);
        } catch (err) {
            reject({ 'Task': `Attempting to pull image from camera (${params.camera.camId}) at ${params.camera.ingest.uri}`, 'Error': err.message, 'Stack': err.stack })
            return;
        }

Specifically, I'm trying to mock the utils.retrieveImageFromURI function - which has API calls and other things in it.

When I try to mock the function using spyOn I am trying it like so:

describe("FUNCTION: ingestAndSave", () => {
    let fakeImageBuffer = Array(1200).fill('a').join('b'); // just get a long string
    console.log(fakeImageBuffer.length) //2399
    let retrieveImageFromURISpy
    beforeAll(() => {
        retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(fakeImageBuffer) 
    })

    test("Will call retrieveImageFromURI", async () => {
        await ingest.ingestAndSave({camera:TEST_CONSTANTS.validCameraObject, sourceQueueURL:"httpexamplecom", receiptHandle: "1234abcd"})
        expect(retrieveImageFromURISpy).toHaveBeenCalledTimes(1)
    })

    afterEach(() => {
        jest.resetAllMocks()
    })
    afterAll(() => {
        jest.restoreAllMocks()
    })
})

When I do this, I get a console log that imageBuffer (which is supposed to be the return of the mocked function) is undefined and that, in turn, triggers the thrown Error that "Retrieve from uri ...." ... which causes my test to fail. I know I could wrap the test call in a try/catch but the very next test will be a "does not throw error" test... so this needs to be solved.

It's not clear to me why the mockReturnValue isn't getting returned.

Other steps:

I've gone to the REAL retrieveImageFromURI function and added a console log - it is not running.

I've changed mockReturnValue to mockImplementation like so:

retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
            console.log("Here")
            return fakeImageBuffer
        }) 

And it does NOT console log 'here'. I'm unsure why not.

I have also tried to return it as a resolved Promise, like so:

retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
    console.log("Here")
    return Promise.resolve(fakeImageBuffer)
}) 

Note, this also doesn't console log.

I've also tried to return the promise directly with a mockReturnValue:

`retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(Promise.resolve(fakeImageBuffer)`) 
lowcrawler
  • 6,777
  • 9
  • 37
  • 79

0 Answers0