I need to check a function ( example()) that uses another ( validateDataset). Since I only want to test the function example(), I am mocking validateDataset().
Of course, each test needs a different result from the mocking function. But how do I set different promise results for the funded feature? In my attempt, shown below, the bullying function always returns the same value.
So, in this example, I cannot verify the abandoned error.
functions.js
import { validateDataset } from './helper/validation'
export async function example (id) {
const { docElement } = await validateDataset(id)
if (!docElement) throw Error('Missing content')
return docElement
}
functions.test.js
import { example } from './functions'
jest.mock('./helper/validation', () => ({
validateDataset: jest.fn(() => Promise.resolve({
docMain: {},
docElement: {
data: 'result'
}
}))
}))
describe('example()', () => {
test('should return object', async () => {
const result = await example('123')
expect(result.data).toBe('result')
})
test('should throw error', async () => {
const result = await example('123')
// How to get different result (`null`) in this test
// How to test for the thrown error?
})
})
source
share