{"id":16399318,"url":"https://github.com/diokuz/puppeteer-request-mocker","last_synced_at":"2025-03-21T02:32:50.191Z","repository":{"id":32551117,"uuid":"136804361","full_name":"Diokuz/puppeteer-request-mocker","owner":"Diokuz","description":"Request mocker for puppeteer","archived":false,"fork":false,"pushed_at":"2023-01-04T21:51:32.000Z","size":1550,"stargazers_count":11,"open_issues_count":18,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-12T05:23:51.999Z","etag":null,"topics":["mocker","puppeteer","request"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Diokuz.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-10T11:50:11.000Z","updated_at":"2021-01-03T17:33:05.000Z","dependencies_parsed_at":"2023-01-14T21:34:14.166Z","dependency_job_id":null,"html_url":"https://github.com/Diokuz/puppeteer-request-mocker","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Diokuz%2Fpuppeteer-request-mocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Diokuz%2Fpuppeteer-request-mocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Diokuz%2Fpuppeteer-request-mocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Diokuz%2Fpuppeteer-request-mocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Diokuz","download_url":"https://codeload.github.com/Diokuz/puppeteer-request-mocker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221811380,"owners_count":16884305,"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":["mocker","puppeteer","request"],"created_at":"2024-10-11T05:23:45.121Z","updated_at":"2024-10-28T09:11:59.920Z","avatar_url":"https://github.com/Diokuz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# puppeteer-request-mocker\n\n\u003e Deprecation warning: puppeteer-request-mocker is deprecated, use [teremock](https://github.com/Diokuz/teremock) instead.\n\n## Migration to teremock\n\nSome options did not changed: `page`, `ci`.\n\nSome options just renamed: `workDir → wd`, `response → responseOverrides`.\n\nOther options were replaced: `queryParams`, `skipQueryParams`, `skipPostParams`, `mockList`, `okList`, `passList`, `mockMiss`, `awaitConnectionsOnStop`.\n\nThese options you need to convert to interceptors. For example, say you have puppeteer-request-mocker configuration:\n\n```js\n{\n  page: browser.page,\n  workDir: 'path/to/dir',\n  mockList: [`https://example.com/my-api`],\n  skipQueryParams: ['randomId'],\n}\n```\n\nThat would transform to\n\n```js\n{\n  page: browser.page,\n  wd: 'path/to/dir',\n  interceptors: {\n    api: {\n      url: 'https://example.com/my-api',\n      name: myApi,\n      naming: {\n        query: { blacklist: ['randomId'] }\n      }\n    }\n  }\n}\n```\n\n___\n\n## Do I need that thing?\n\nIf you are writing puppeteer tests, and you want to mock your network responses easily – probably yes.\n\n## How to use\n\n```js\nimport mocker from 'puppeteer-request-mocker'\n\nawait mocker.start()\n\n// async stuff which is making requests\n\nawait mocker.stop()\n```\n\n## How it works\n\nFirst, `puppeteer-request-mocker` intercepts puppeteers page requests and tries to find its body in mocks folder. Generated filename depends on `url`, `method` and `postBody` – so, you always know, do you have a mock for that particular request or not. If you have it – you will get it as a response, instantly. If not – request will go to the real backend (see also: mockList and okList).\n\nSecond, `puppeteer-request-mocker` intercepts all responds, and writes them to the filesystem, if they are not on it already. In case of `CI` (if mock was not found), it uses mockMiss middleware, so you could be sure – all your requests are mocked (or build will fail otherwise).\n\n## Pipeline\n\n\u003cimg src=\"assets/pipeline.svg\" /\u003e\n\n## API\n\nYou could use `options`\n```js\nmocker.start(options)\n```\nAll options are optional (that's why they called so).\n```js\nconst options = {\n  // Absolute path to folder where you want to store mocks\n  // process.cwd() + '/__remocks__' by default\n  workDir: __dirname,\n\n  // puppeteer page\n  // global.page by default\n  page: page,\n\n  // In some cases you could have some random GET params, which are not affect the response body\n  // but several params may be important for you (White List)\n  // [] by default\n  queryParams: ['important'],\n\n  // In some cases you could have some random GET params, which are not affects the response body\n  // but could lead to `always out of date` mocks (Black List)\n  // [] by default\n  skipQueryParams: ['randomId', 'timestamp'],\n\n  // Same as skipQueryParams but for post body params\n  // Only application/json MIME type is supported\n  skipPostParams: [\n      'randomId',\n      'timestamp',\n      ['objectParameter', 'property']\n  ],\n\n  // Probably you dont want to mock some requests (e.g. cdn js files)\n  // And you definitely dont want to mock your webapp requests (e.g. localhost/app.js)\n  // So, you could explicitly whitelist urls you want to mock\n  // _all except localhost_ if both – mockList and okList – were not set\n  // Could be an array, or a `,` delimited string\n  mockList: 'my-backend.org/used/by/test',\n\n  // It is recommended to explicitly mock only _critical-for-your-test_ urls\n  // But you could also mock with simple 200-OK response some other requests,\n  // which are not critical, but should be intercepted\n  // (to prevent ddos-effect to your real backend, for example)\n  // All items from mockList have higher priority over okList\n  // Could be an array, or a `,` delimited string\n  okList: ['my-backend.org/not/critical/for/test'],\n\n  // If url not in mockList, nor okList, it will be blocked, unless any of two conditions\n  // 1) url is same origin, and method is GET\n  // 2) url is matched agains any string in passList\n  // By default, block any cross origin and non-GET same-origin requests\n  passList: [],\n\n  // Run as CI if true. That means, your tests will fail if any of the requests were not mocked\n  // Default is `is-ci` package value (same as in Jest)\n  ci: require('is-ci'),\n\n  // A middleware to call when mock is not found on the file system\n  // Works only in CI mode\n  // Possible values are:\n  // 1) CODE (number) – respond with CODE http code for any unmocked request (e.g. 200)\n  // 2) 'throw' (string) – will throw an error\n  // 3) (next) =\u003e next(anyResponse) - respond with anyResponse object\n  // default value is: 500\n  // Note: request is not available in the middleware function\n  // Note: body must be a string (use JSON.stringify for objects)\n  mockMiss: (next) =\u003e next({ code: 200, body: JSON.stringify({ foo: 'bar' }) }),\n\n  // Set true, to await all non-closed connections when trying to stop mocker\n  // Warning: some tests could became flaky\n  awaitConnectionsOnStop: false,\n\n  // Custom headers or/and body or/and status for ANY request from the mockList\n  // All keys in the object are optional (e.g. you could change only status code)\n  // Usefull with combination of mocker.set() method\n  // Warning! It is not working in the mocks generation mode! So, first, create your mocks.\n  // See also https://github.com/puppeteer/puppeteer/issues/599\n  response: {\n    headers: { 'Access-Control-Allow-Origin': '*' },\n    body: 'OK',\n    status: 200,\n  },\n}\n```\nBoth `mocker.start()` and `mocker.stop()` return a `Promise`.\n\n## mocker.set()\n\nYou could temporary change any option and then get back to its initial value.\n\nFor example:\n\n```js\nmocker.set('response', { status: 427 })\nconst result = await doRequest()\nexpect(result).toBe('Server responded with an error, status code is 427')\n// note: headers and body will be taken from the mocks, only status code is changed\nmocker.unset('response) // or mocker.reset()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiokuz%2Fpuppeteer-request-mocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiokuz%2Fpuppeteer-request-mocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiokuz%2Fpuppeteer-request-mocker/lists"}