DRFR0ST / bun-bagel

🥯 The Bun fetch mocker with a hole lot of flavor.
MIT License
18 stars 5 forks source link

Call ORIGINAL_FETCH when no requests match #12

Open aryzing opened 2 months ago

aryzing commented 2 months ago

When adding a single API endpoint mock(), any other fetch requests (even to different hosts) will fail. This is impractical for large codebases with many fetch requests, where only one or a few requests need to be mocked. It would be very useful if we could configure the behavior for unmatched requests to use ORIGINAL_FETCH instead. It certainly would help porting large code bases.

https://github.com/DRFR0ST/bun-bagel/blob/3793f8233acb981657ea3f243aa110bf6adb2bc0/src/mock.ts#L77-L78

Something like,

if (!mockedRequest)
  if (fallBackToOriginalFetchWhenNoMockMatchesFound)
    return ORIGINAL_FETCH(/*...*/)
  else 
    return Promise.reject(makeResponse(404, _path));
aryzing commented 2 months ago

If it helps, this is how nock handles allowing real network requests: https://www.npmjs.com/package/nock?activeTab=readme#enabledisable-real-http-requests

DRFR0ST commented 1 month ago

Thanks for pointing this out! The fix is about to be merged in this PR: #18