{"id":23822243,"url":"https://github.com/liqueurdetoile/fetch-mock-fixtures","last_synced_at":"2025-09-07T04:31:40.682Z","repository":{"id":43414733,"uuid":"177683531","full_name":"liqueurdetoile/fetch-mock-fixtures","owner":"liqueurdetoile","description":"FMF provides an easy way to mock fetch API calls and allow the use of fixtures","archived":false,"fork":false,"pushed_at":"2024-09-13T10:26:53.000Z","size":5367,"stargazers_count":0,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-16T07:17:23.177Z","etag":null,"topics":["fetch","fetch-api","fixture-loader","fixtures","mock"],"latest_commit_sha":null,"homepage":"https://liqueurdetoile.github.io/fetch-mock-fixtures/","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/liqueurdetoile.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","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-03-26T00:04:18.000Z","updated_at":"2022-02-19T14:26:12.000Z","dependencies_parsed_at":"2024-09-11T13:54:46.881Z","dependency_job_id":"01c79714-74ad-4888-af64-c3c5dafc8321","html_url":"https://github.com/liqueurdetoile/fetch-mock-fixtures","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liqueurdetoile%2Ffetch-mock-fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liqueurdetoile%2Ffetch-mock-fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liqueurdetoile%2Ffetch-mock-fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liqueurdetoile%2Ffetch-mock-fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liqueurdetoile","download_url":"https://codeload.github.com/liqueurdetoile/fetch-mock-fixtures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232175019,"owners_count":18483501,"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":["fetch","fetch-api","fixture-loader","fixtures","mock"],"created_at":"2025-01-02T09:16:23.397Z","updated_at":"2025-01-02T09:16:24.094Z","avatar_url":"https://github.com/liqueurdetoile.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Build status](https://github.com/liqueurdetoile/fetch-mock-fixtures/actions/workflows/test-suite.yml/badge.svg?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/liqueurdetoile/fetch-mock-fixtures/badge.svg?branch=master)](https://coveralls.io/github/liqueurdetoile/fetch-mock-fixtures?branch=master)\n[![Documentation](https://liqueurdetoile.github.io/fetch-mock-fixtures/badge.svg)](https://liqueurdetoile.github.io/fetch-mock-fixtures/)\n[![Known Vulnerabilities](https://snyk.io/test/github/liqueurdetoile/fetch-mock-fixtures/badge.svg)](https://snyk.io/test/github/liqueurdetoile/fetch-mock-fixtures)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n\n\u003cp align=\"center\"\u003e\u003ca href=\"https://liqueurdetoile.com\" target=\"\\_blank\"\u003e\u003cimg src=\"https://hosting.liqueurdetoile.com/logo_lqdt.png\" alt=\"Liqueur de Toile\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n# Fetch-mock-fixtures (FMF)\nWhile most of mockers for fetch are only meant to intercept and define the next response content, FMF goes far beyond and offers a wide range of tools for testing js apps.\n\nWhat is featured :\n- BDD style syntax to configure server and fixtures in a more readable-friendly way. It's heavily inspired by [Chai `should` assertion library](https://www.chaijs.com/guide/styles/#should),\n- Easy way to configure response on-the-fly, mutating responses from call to call...\n- Enhanced [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) native object to automatically parse url details and query content with [`url-parse`](https://github.com/unshiftio/url-parse#readme),\n- Powerful response presets and fixtures system to avoid writing the same things again and again and ease functional testing,\n- Easy access to the full history or requests/responses handled by the server since its first start or last reset,\n- Parametrized request thanks to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp#readme) to enable dynamic fixtures routing in a few lines of code\n- and many more !\n\nFor instance, with FMF, you can do such things to quickly configure two fixtures in a mocha test (that will obviously succeed) :\n\n```javascript\nimport Server from 'fetch-mock-fixtures';\n\nconst server = new Server();\n\ndescribe('Headers test', function() {\n  before(() =\u003e server.start()) // Start intercepting fetch calls\n  after(() =\u003e server.stop()) // Restore to normal behavior\n\n  it('should use json headers', async function() {\n    server\n      .when // or .on\n        .header('content-type').equal(/json/) // use a regexp here to avoid writing full header\n        .respond.with.preset('200')\n      .fallback.to.preset('400')\n\n    let response = await fetch('/', {\n      headers: {'content-type': 'application/json'}\n    });\n\n    response.status.should.equal(200);\n  })\n})\n```\n**How FMF can ease API outgoing requests unit tests ?**\n\nFMF enables really quick response configuration that allows testing the outgoing request to set up different responses (see above example). You only have to check a response property (like status) instead of manually parsing request built by your app to validate it.\n\nFurthermore, you can use the `before` and `after` hooks or `body` as a callback to alter response on very precise expectations.\n\n**How FMF can ease functional tests ?**\n\nIn real life, scripts are often sending multiple requests to do their job. FMF removes the pain of handling multiple responses by easing their management. Let's see this example with a two steps authentication login. A bit verbose for what is actually doing but it aims to illustrate things :\n\n```javascript\nimport Server from 'fetch-mock-fixtures';\n\nconst server = new Server();\n\n// Define on-the-fly fixtures to handle login tests\nserver\n  .verbose(true) // Enable console login for each request/response/error\n  .when\n      .pathname.equal('/login')\n      .method.equal('POST')\n      .body('json').equal(body =\u003e body.username === 'foo')\n    .respond.with\n      .preset(401)\n  .when\n      .pathname.equal('/session')\n      .method.equal('POST')\n      .body('json').equal(token =\u003e body.authToken === '123')\n    .respond.with\n      .preset('json')\n      .body({success: true, sessionToken: '456'})\n  .fallback.to\n    .preset(403)\n\ndescribe('Login test suite', function() {\n  before(() =\u003e server.start())\n  after(() =\u003e server.stop())\n\n  it('should login', async function() {\n    await triggerTheLoginLogic('foo');\n    await sendTheTokenLogic('123');\n    logged.should.be.true;\n  })\n\n  it('should fail login on username', async function() {\n    await triggerTheLoginLogic('bar');\n    logged.should.be.false;\n  })\n\n  it('should fail login on token', async function() {\n    await triggerTheLoginLogic('foo');\n    await sendTheTokenLogic('hacked!');\n    logged.should.be.false;\n  })\n})\n```\nWe're not only sending back data to the app but also checking outgoing requests at the same time because the answer will only be sent if calling the right url with the right method and the right data. `with` and `to` are only optional sugars to improve human readability.\n\nLast not least, you can easily deploy url-based routing to use your \"real\" data inside each tests instead of providing fake data and get rid of on-the-fly fixtures (see [dynamic fixtures examples](https://liqueurdetoile.github.io/fetch-mock-fixtures/manual/dynamic-fixtures-examples)).\n\n**When to use FMF ?**\n\nAt any time :smile:\n\nNevertheless, FMF will truly give its best with any testing framework (Mocha, Jasmine, Junit...) that allows to automate operations between each tests like `start`, `stop` or `reset` the server.\n\n## Installation\n\nInstallation can easily be done through NPM or Yarn. Sinon is required by FMF to stub `fetch` but is not included in the bundle. It must be installed as well if not already present.\n\n```bash\nnpm install sinon fetch-mock-fixtures --save-dev\n\nyarn add sinon fetch-mock-fixtures --dev\n```\nFMF should be installed as a dev dependency. It is not meant to be used as an in-app offline mode feature.\n\n**Note** : FMF is built upon Promise, Proxy and fetch API (Request, Headers, Response) that are available in all modern browsers. If you intend to run tests on older browsers (IE) or versions, you may need to polyfill them. Here's some available tools you can use :\n- Promise: [ES6-Promise](https://www.npmjs.com/package/es6-promise)\n- Fetch API : [window.fetch polyfill](https://www.npmjs.com/package/whatwg-fetch)\n- Proxy: [proxy-polyfill](https://www.npmjs.com/package/proxy-polyfill)\n\n## Full documentation and API reference\nPlease pay visit to [the docs pages](https://liqueurdetoile.github.io/fetch-mock-fixtures/).\n\n## Bugs and improvements\nAny bugs and issues can be filed on the [github repository](https://github.com/liqueurdetoile/fetch-mock-fixtures/issues).\n\nYou are free and very welcome to fork the project and submit any PR to fix or improve FMF.\n\nI'm especially interested for good will who wish to improve query matcher processors to provide more tools to evaluate query and choose the right response.\n\n## Changelog\n- 2.2.0 : Add global preset configuration within server instance and throw behavior for fixture\n- 2.1.0 : Add history logging and verbose mode\n- 2.0.0 : BREAKING CHANGE - A brand new FMF highly **not** compatible with previous version\n- 1.0.1 : Add requests history and possibility to set up different responses based on requests order. Add delay as a response parameter into fixture.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliqueurdetoile%2Ffetch-mock-fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliqueurdetoile%2Ffetch-mock-fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliqueurdetoile%2Ffetch-mock-fixtures/lists"}