{"id":13724987,"url":"https://github.com/wheresrhys/fetch-mock-jest","last_synced_at":"2025-08-08T13:31:57.025Z","repository":{"id":35863628,"uuid":"219860062","full_name":"wheresrhys/fetch-mock-jest","owner":"wheresrhys","description":"Jest wrapper for fetch-mock, a comprehensive stub for fetch","archived":false,"fork":false,"pushed_at":"2024-09-10T13:14:52.000Z","size":261,"stargazers_count":60,"open_issues_count":17,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-14T18:03:35.615Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.wheresrhys.co.uk/fetch-mock/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wheresrhys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-05T22:10:42.000Z","updated_at":"2024-08-08T13:34:01.000Z","dependencies_parsed_at":"2024-10-25T18:43:17.659Z","dependency_job_id":"e7b079a7-4449-40b1-ac35-617ab9255d92","html_url":"https://github.com/wheresrhys/fetch-mock-jest","commit_stats":{"total_commits":53,"total_committers":4,"mean_commits":13.25,"dds":0.3207547169811321,"last_synced_commit":"27e8eaaa74b53f30b10c0137346a8011fd4e51fe"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wheresrhys%2Ffetch-mock-jest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wheresrhys%2Ffetch-mock-jest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wheresrhys%2Ffetch-mock-jest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wheresrhys%2Ffetch-mock-jest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wheresrhys","download_url":"https://codeload.github.com/wheresrhys/fetch-mock-jest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229144637,"owners_count":18026911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:08.889Z","updated_at":"2024-12-10T23:04:05.224Z","avatar_url":"https://github.com/wheresrhys.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\n**Deprecated - Use https://www.npmjs.com/package/@fetch-mock/jest instead. This upgrade guide will help upgrade the underlying fetch-mock instance https://www.wheresrhys.co.uk/fetch-mock/docs/Usage/upgrade-guide**\n\n# fetch-mock-jest\n\nWrapper around [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock) - a comprehensive, isomorphic mock for the [fetch api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) - which provides an interface that is more idiomatic when working in [jest](https://jestjs.io).\n\nThe example at the bottom of this readme demonstrates the intuitive API, but shows off only a fraction of fetch-mock's functionality. Features include:\n\n- mocks most of the fetch API spec, even advanced behaviours such as streaming and aborting\n- declarative matching for most aspects of a http request, including url, headers, body and query parameters\n- shorthands for the most commonly used features, such as matching a http method or matching one fetch only\n- support for delaying responses, or using your own async functions to define custom race conditions\n- can be used as a spy to observe real network requests\n- isomorphic, and supports either a global fetch instance or a locally required instanceg\n\n# Requirements\n\nfetch-mock-jest requires the following to run:\n\n- [Node.js](https://Node.js.org/) 8+ for full feature operation\n- [Node.js](https://Node.js.org/) 0.12+ with [limitations](http://www.wheresrhys.co.uk/fetch-mock/installation)\n- [npm](https://www.npmjs.com/package/npm) (normally comes with Node.js)\n- [jest](https://www.npmjs.com/package/jest) 25+ (may work with earlier versions, but untested)\n- Either\n  - [node-fetch](https://www.npmjs.com/package/node-fetch) when testing in Node.js. To allow users a choice over which version to use, `node-fetch` is not included as a dependency of `fetch-mock`.\n  - A browser that supports the `fetch` API either natively or via a [polyfill/ponyfill](https://ponyfoo.com/articles/polyfills-or-ponyfills)\n\n# Installation\n\n`npm install -D fetch-mock-jest`\n\n## global fetch\n\n`const fetchMock = require('fetch-mock-jest')`\n\n## node-fetch\n\n```\njest.mock('node-fetch', () =\u003e require('fetch-mock-jest').sandbox())\nconst fetchMock = require('node-fetch')\n```\n\n# API\n\n## Setting up mocks\n\nPlease refer to the [fetch-mock documentation](http://wheresrhys.co.uk/fetch-mock) and [cheatsheet](https://github.com/wheresrhys/fetch-mock/blob/master/docs/cheatsheet.md)\n\nAll jest methods for configuring mock functions are disabled as fetch-mock's own methods should always be used\n\n## Inspecting mocks\n\nAll the built in jest function inspection assertions can be used, e.g. `expect(fetchMock).toHaveBeenCalledWith('http://example.com')`.\n\n`fetchMock.mock.calls` and `fetchMock.mock.results` are also exposed, giving access to manually inspect the calls.\n\nThe following custom jest expectation methods, proxying through to `fetch-mock`'s inspection methods are also available. They can all be prefixed with the `.not` helper for negative assertions.\n\n- `expect(fetchMock).toHaveFetched(filter, options)`\n- `expect(fetchMock).toHaveLastFetched(filter, options)`\n- `expect(fetchMock).toHaveNthFetched(n, filter, options)`\n- `expect(fetchMock).toHaveFetchedTimes(n, filter, options)`\n- `expect(fetchMock).toBeDone(filter)`\n\n### Notes\n\n- `filter` and `options` are the same as those used by [`fetch-mock`'s inspection methods](http://www.wheresrhys.co.uk/fetch-mock/#api-inspectionfundamentals)\n- The obove methods can have `Fetched` replaced by any of the following verbs to scope to a particular method: + Got + Posted + Put + Deleted + FetchedHead + Patched\n\ne.g. `expect(fetchMock).toHaveLastPatched(filter, options)`\n\n## Tearing down mocks\n\n`fetchMock.mockClear()` can be used to reset the call history\n\n`fetchMock.mockReset()` can be used to remove all configured mocks\n\nPlease report any bugs in resetting mocks on the [issues board](https://github.com/wheresrhys/fetch-mock-jest/issues)\n\n# Example\n\n```js\nconst fetchMock = require('fetch-mock-jest');\nconst userManager = require('../src/user-manager');\n\ntest(async () =\u003e {\n\tconst users = [{ name: 'bob' }];\n\tfetchMock\n\t\t.get('http://example.com/users', users)\n\t\t.post('http://example.com/user', (url, options) =\u003e {\n\t\t\tif (typeof options.body.name === 'string') {\n\t\t\t\tusers.push(options.body);\n\t\t\t\treturn 202;\n\t\t\t}\n\t\t\treturn 400;\n\t\t})\n\t\t.patch(\n\t\t\t{\n\t\t\t\turl: 'http://example.com/user'\n\t\t\t},\n\t\t\t405\n\t\t);\n\n\texpect(await userManager.getAll()).toEqual([{ name: 'bob' }]);\n\texpect(fetchMock).toHaveLastFetched('http://example.com/users\n\t\tget');\n\tawait userManager.create({ name: true });\n\texpect(fetchMock).toHaveLastFetched(\n\t\t{\n\t\t\turl: 'http://example.com/user',\n\t\t\tbody: { name: true }\n\t\t},\n\t\t'post'\n\t);\n\texpect(await userManager.getAll()).toEqual([{ name: 'bob' }]);\n\tfetchMock.mockClear();\n\tawait userManager.create({ name: 'sarah' });\n\texpect(fetchMock).toHaveLastFetched(\n\t\t{\n\t\t\turl: 'http://example.com/user',\n\t\t\tbody: { name: 'sarah' }\n\t\t},\n\t\t'post'\n\t);\n\texpect(await userManager.getAll()).toEqual([\n\t\t{ name: 'bob' },\n\t\t{ name: 'sarah' }\n\t]);\n\tfetchMock.mockReset();\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwheresrhys%2Ffetch-mock-jest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwheresrhys%2Ffetch-mock-jest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwheresrhys%2Ffetch-mock-jest/lists"}