Skip to content

Conversation

@RoCat
Copy link

@RoCat RoCat commented Nov 28, 2024

When we just mock one route, if we have more requests than the one mocked, they dont have .route in callHistory. that result in JS error (destructured parameter).route is undefined

    fetchMock.mockGlobal();
    fetchMock.put('/documents', 200, 'updateDocuments');
    // remove line below and then its not working anymore
    fetchMock.route('*', 200);

    await $('>>>.chip.contract').click();
    await fetchMock.callHistory.flush(true);

    expect(
      fetchMock.callHistory.called('updateDocuments')
    ).toBe(true);

I may miss something, but the code change seems to fix this issue

@wheresrhys
Copy link
Owner

Try using fetchMock.catch() instead of fetchMock.route('*', 200) - that would be the advised approach

Can I also ask what error you get exactly when you don't include the catch all route? Is it thrown from inside the call to flush()? fetch-mock should end up here https://github.com/wheresrhys/fetch-mock/blob/main/packages/fetch-mock/src/Router.ts#L208, but as you're using flush instead of await perhaps that error isn't bubbling up, making it a bit confusing

@wheresrhys
Copy link
Owner

closing due to inactivity

@wheresrhys wheresrhys closed this Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants