{"id":13496473,"url":"https://github.com/spotify/polly-jest-presets","last_synced_at":"2025-03-28T18:32:04.787Z","repository":{"id":41432232,"uuid":"197635102","full_name":"spotify/polly-jest-presets","owner":"spotify","description":null,"archived":true,"fork":false,"pushed_at":"2022-04-09T03:22:53.000Z","size":3658,"stargazers_count":64,"open_issues_count":25,"forks_count":10,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-13T17:54:34.436Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spotify.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}},"created_at":"2019-07-18T18:09:41.000Z","updated_at":"2024-03-29T09:50:49.000Z","dependencies_parsed_at":"2022-09-26T16:21:17.443Z","dependency_job_id":null,"html_url":"https://github.com/spotify/polly-jest-presets","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spotify%2Fpolly-jest-presets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spotify%2Fpolly-jest-presets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spotify%2Fpolly-jest-presets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spotify%2Fpolly-jest-presets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spotify","download_url":"https://codeload.github.com/spotify/polly-jest-presets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246080773,"owners_count":20720590,"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-07-31T19:01:48.396Z","updated_at":"2025-03-28T18:32:03.866Z","avatar_url":"https://github.com/spotify.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# polly-jest-presets\n\n[![Actions Status](https://github.com/spotify/polly-jest-presets/workflows/Tests/badge.svg)](https://github.com/spotify/polly-jest-presets/actions)\n[![Version](https://img.shields.io/npm/v/@spotify/polly-jest-presets.svg)](https://www.npmjs.com/package/@spotify/polly-jest-presets)\n\nAn opinionated configuration and wrapper around [Polly] and [setup-polly-jest] to have automatic recording and playback of network requests made during your [Jest] tests.\n\n**Note:** Polly Jest Presets bundles in all necessary Polly packages to make the setup as easy as possible for a typical Node-based web app. It uses File persister to store recordings locally. Therefore, there's no need to install Polly or Polly Jest bindings separately in your project after including this preset.\n\n**Packages included:**\n\n```json\n\"@pollyjs/adapter-node-http\": \"^2.6.0\",\n\"@pollyjs/core\": \"^2.6.0\",\n\"@pollyjs/persister-fs\": \"^2.6.0\",\n\"setup-polly-jest\": \"^0.5.2\",\n```\n\n## Usage\n\nInstall the preset as a dev dependency:\n\n```sh\nyarn add @spotify/polly-jest-presets -D\n```\n\nAdd the preset to your [Jest config](https://jestjs.io/docs/en/configuration) (by default `jest.config.js`), in the `setupFilesAfterEnv`:\n\n```json\n{\n  \"setupFilesAfterEnv\": [\"@spotify/polly-jest-presets\"]\n}\n```\n\nOr import in an individual test.\n\n```js\n// ./my.test.js\nimport '@spotify/polly-jest-presets';\n```\n\n### Getting Started\n\nTo test it out, make a network request in one of your tests.\n\n```js\nimport '@spotify/polly-jest-presets';\n// `yarn add -D node-fetch` for this demo\nimport fetch from 'node-fetch';\n\ndescribe('a dummy test', () =\u003e {\n  it('fetches something', async () =\u003e {\n    const resp = await fetch('https://reqres.in/api/users?page=2');\n    const payload = await resp.json();\n    expect(payload.data.length).toBeGreaterThan(1);\n  });\n});\n```\n\nFirst, you need to run the tests with the `POLLY_MODE` environment variable set to `record`. This will tell Polly that you intend for all of the requests to record in this test run.\n\n```sh\nPOLLY_MODE=\"record\" jest\n```\n\nYou should now see a `__recordings__` directory next to your test file. It should contain a `.har` file which shows the request we made within the it block.\n\nTo test that playback works, disconnect your internet on your machine and run:\n\n```sh\nPOLLY_MODE=\"replay\" jest\n```\n\nThe test still passes! _Note: the default POLLY_MODE is `replay`._\n\n### Configuration and API usage\n\nIf you want to override Polly configuration, you can add configuration to `globals.pollyConfig` in the Jest config:\n\n```json\n{\n  \"globals\": {\n    \"pollyConfig\": {\n      \"expiresIn\": \"3 months\"\n    }\n  }\n}\n```\n\n_See all of the valid Polly options [in the Polly documentation](https://netflix.github.io/pollyjs/#/configuration)._\n\nYou may also want to get at the global Polly instance. You can import it:\n\n```js\n// if you need access to the pollyContext\nimport { pollyContext } from '@spotify/polly-jest-presets';\n\npollyContext.polly.server\n  .get('/series')\n  .intercept((req, res) =\u003e res.sendStatus(200));\n```\n\n_See all of the Polly API methods [in the Polly documentation](https://netflix.github.io/pollyjs/#/api)._\n\n## Opinions\n\nThis preset has a few opinions baked in. All of these are overridable by setting the `globals.pollyConfig` in your Jest config.\n\n### Expire recordings often\n\nWe think it's safer to expire recordings frequently. The default `expiresIn` is set to `\"14d\"` in this preset. By default, `expiryStrategy` is configured to `warn`.\n\n### Explicit recording only (no `recordIf*`)\n\nWe think it makes more sense to avoid recording in the background for test authoring to avoid unexpected changes to checked in .har files. Because of this, we have set all of the `recordIf*` config values to false. **This means that your tests will fail when you first write them if you don't override `POLLY_MODE`.**\n\n## Contributing\n\nSee [CONTRIBUTING](./CONTRIBUTING.md) guidelines.\n\n[polly]: https://netflix.github.io/pollyjs\n[setup-polly-jest]: https://www.npmjs.com/package/setup-polly-jest\n[eslint]: https://eslint.org/\n[jest]: http://jestjs.io/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspotify%2Fpolly-jest-presets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspotify%2Fpolly-jest-presets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspotify%2Fpolly-jest-presets/lists"}