Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: route may be undefined when no match #883

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

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