简体   繁体   中英

Node Jest mocking a function in a function

I am Testing a method like such:

test('Test', async () => {
    const result = await methodBeingTested(someData);
    })
})

Then the method I am testing looks something like this:

import { insertData } from '../util/api';
export async function methodBeingTested(someData: any) {
    const data = await insertData(dateIdsomeData)

    //Do Some other stuff
    return data
}

I would like to mock the data insertData method returns just so it doesn't do the axios request or the insert into mongo / sql.

How am I able to do this with Jest? I am mainly just wanting to test the functionality of the other stuff going on in the methodBeingTested .

Thanks

Do this:

jest.mock('../util/api', () => ({
   insertData: () => Promise.resolve(mockResult) // put here some result 
}));

// your tests

Just make sure that your path is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM