包详细信息

fetch-mock

wheresrhys3.5mMIT12.5.2

Mock http requests made using fetch

fetch, http, mock, testing

自述文件

fetch-mock

Features include:

  • mocks most of the fetch API spec, even advanced behaviours such as streaming and aborting
  • declarative matching for most aspects of a http request, including url, headers, body and query parameters
  • shorthands for the most commonly used features, such as matching a http method or matching one fetch only
  • support for delaying responses, or using your own async functions to define custom race conditions
  • can be used as a spy to observe real network requests
  • can be extended with your own reusable custom matchers that can be used both for matching fetch-calls and inspecting the results
  • isomorphic, and supports either a global fetch instance or a locally required instance

Requirements

@fetch-mock requires either of the following to run:

  • Node.js 18+ for full feature operation
  • Any modern browser that supports the fetch API
  • node-fetch when testing in earlier versions of Node.js (this is untested, but should mostly work)

Documentation and Usage

See the project website

License

fetch-mock is licensed under the MIT license. Copyright © 2024, Rhys Evans

更新日志

Changelog

12.5.2 (2025-03-03)

Bug Fixes

  • allow matching body for delete requests (891197c)

12.5.1 (2025-03-01)

Bug Fixes

  • improve handling of abort (5c1085d)
  • remove deprecated is-subset-of (a2546ef)

12.5.0 (2025-03-01)

Features

12.4.0 (2025-02-23)

Features

  • add ability to wait for multiple routes (c3dc9c3)
  • implement waitFor option (5500228)

Bug Fixes

  • clone response before using (2ccf18e)
  • use a promise, no function, to implement waitFor (8783101)

12.3.0 (2025-02-04)

Features

  • added overwriteRoutes: true rule to codemods (b3d1468)
  • implemented modifyRoute() method (f62e3d6)
  • implemented removeRoute method (584a861)

Bug Fixes

  • make types for modify route config more accurate (6894569)
  • prevented overwriting stickiness of a route (87f8c9b)

12.2.1 (2025-01-28)

Bug Fixes

  • fix failure to spy in browsers (bfaa5f3)

12.2.0 (2024-11-15)

Features

  • implement new hardReset method (d7e0776)
  • update codemods to use hardReset() (757d480)

12.1.0 (2024-11-08)

Features

  • fetch-mock: add include: matcher for urls (02f880c)

12.0.3 (2024-11-08)

Bug Fixes

  • allow matching relative URLs (2cba1bc)

12.0.2 (2024-10-28)

Bug Fixes

  • allow sending responses with status 0 (92c06e9)

12.0.1 (2024-10-27)

Bug Fixes

  • clearHistory() can deal with unmatched calls (012e9ca)

12.0.0 (2024-10-24)

⚠ BREAKING CHANGES

  • Replaced legacy fetch-mock code with fetch-mock/core code

Features

  • Replaced legacy fetch-mock code with fetch-mock/core code (999ce92)

0.7.1 (2024-09-25)

Bug Fixes

  • change export order so default is last (bc9c41d)

0.7.0 (2024-08-30)

⚠ BREAKING CHANGES

  • remove sendAsJson option

Features

  • remove sendAsJson option (4b11fc4)
  • send content=length header whenever calculable (ef4c684)

Bug Fixes

  • force engine to be >=18.11.0 as this fixes an issue in proxying a response (dde5e6b)
  • handle all types of BodyInit correctly (0242ea2)

0.6.3 (2024-08-29)

Documentation Changes

  • minor corrections (d77978a)
  • readme for @fetch-mock/core (5fd7c5a)
  • wrote a README for @fetch-mock/vitest (d2d1ea3)

0.6.2 (2024-08-28)

Bug Fixes

  • add missing metadata to package.json files (4ab78b9)

0.6.1 (2024-08-15)

Features

  • export more types from @fetch-mock/core (2bff326)

0.6.0 (2024-08-13)

⚠ BREAKING CHANGES

  • force a major release due to breaking nature of relative url API changes

Features

  • implemented allowRelativeUrls option (f32bd6e)

Bug Fixes

  • all relative url behaviour is as expected now; (dc99eb7)
  • force a major release due to breaking nature of relative url API changes (6f29db8)

0.5.0 (2024-08-13)

⚠ BREAKING CHANGES

  • implemented desired behaviour for dot path matching
  • implemented desired behaviour for protocol relative urls

Features

  • implemented desired behaviour for dot path matching (b2fe6f8)
  • implemented desired behaviour for protocol relative urls (06e6260)

0.4.12 (2024-08-13)

Bug Fixes

  • roll back to glob-to-regexp (b114124)

0.4.11 (2024-08-09)

Bug Fixes

0.4.10 (2024-08-08)

Bug Fixes

0.4.9 (2024-08-08)

Bug Fixes

  • add license file to each package (9b36f89)

0.4.8 (2024-08-03)

Documentation Changes

  • document and test behaviour with multiple missing headers (88d0440)

0.4.7 (2024-08-02)

Bug Fixes

  • correct types so that global optiosn can be passed in to route (13e1fc6)

0.4.6 (2024-07-30)

Bug Fixes

  • now more spec compliant on exceptions (ceec07f)

0.4.5 (2024-07-26)

Features

  • allow spying on just one route (a9638fc)
  • rename restoreGlobal to unmockGlobal (3ad4241)

0.4.4 (2024-07-25)

Features

  • cancel readable streams as effectively as possible (aa3b899)
  • support multiple url matchers at once (c83d9f9)

0.4.3 (2024-07-24)

Bug Fixes

  • make a more sensible decision about matching body (0ef50d6)

0.4.2 (2024-07-24)

Features

  • make query parameters available on CallLog (8ec57ac)

0.4.0 (2024-07-24)

⚠ BREAKING CHANGES

  • defined route shorthand methods more declaratively

refactor

  • defined route shorthand methods more declaratively (f42d240). This includes removing all the ${method}Any() and ${method}AnyOnce() methods.

0.3.1 (2024-07-23)

Documentation Changes

  • fixed tests and documented the async behaviour (664a6df)

0.3.0 (2024-07-21)

⚠ BREAKING CHANGES

  • matchers now take normalized requests as input
  • renamed func to matcherFunction
  • removed support for passing in a matcher under the generic name matcher
  • renamed functionMatcher to func

refactor

  • matchers now take normalized requests as input (da9dfe8)

Features

  • can now access express parameters in responses (41e2475)
  • removed support for passing in a matcher under the generic name matcher (f41d8f9)
  • renamed func to matcherFunction (e5679a7)
  • renamed functionMatcher to func (4cee629)
  • response builder function now expects a calllog (306357d)

0.2.0 (2024-07-20)

⚠ BREAKING CHANGES

  • removed top level done and flush methods

Features

  • removed top level done and flush methods (49ae6f7)

Bug Fixes

  • callhistory created with instance.config, not this.config (87206e6)

0.1.1 (2024-07-18)

Features

  • wip: replace dequal, glob-to-regexp and bump path-to-regexp (d8d8b25)

Bug Fixes

  • replace path-to-regexp with regexparam (4bf3e32)

0.1.0 (2024-07-15)

Bug Fixes

  • install core package dependencies (9c73e76)