I want to write a test for an async function that fetches data from an external API.
Jest documentation tells me to return a promise for the test case like so:
test('the data is peanut butter', () => {
return fetchData().then(data => {
expect(data).toBe('peanut butter')
})
})
My problem with this approach is that it makes a separate fetchData
call for each test case. But there is no guarantee that two API calls will return the same data, thus each test case might be running over a different data set.
I would like to make the fetchData
call once and then run all my tests on the same response data - something like this:
describe('fetchData works as expected', () => {
fetchData().then(data => {
// test1
test('the data is peanut butter', () => {
expect(data).toBe('peanut butter')
})
// test2
test('the data is peanut butter', () => {
expect(data).toBe('peanut butter')
})
})
})
How do I do that?
Aucun commentaire:
Enregistrer un commentaire