Skip to content

Testing flow functions in Jest #2032

Answered by clgeoio
Claudiaapalagie asked this question in Q&A
Discussion options

You must be logged in to vote

You should be able to remove the then requirement as the yield returns the resolved value. The syntax is very similar to async/await

 const someFetch = yield axios.get(`${config.apiEndpoint.endpoint}/${param1}`, { headers: header })
 self.instanceofSometing = Modela.create(someFetch.data);

In testing you could jest.mock('axios') then mock the resolvedValue() of axios something like:

const mockAxios = {
 get: jest.fn()
};

jest.mock('axios', ()=>mockAxios)
...

it('does the thing', async ()=>{
  mockAxios.get.mockResolveValue({ data: 'foobar' })
  await store.fetchSomething('one', 'two');
  expect(store.instanceOfSometing.data).toEqual('foobar');

Hopefully that pseudocode should point yo…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by coolsoftwaretyler
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@coolsoftwaretyler
Comment options

@coolsoftwaretyler
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1835 on June 27, 2023 04:18.