{"library":"setup-polly-jest","title":"PollyJS Jest Helper","description":"setup-polly-jest provides a testing helper that integrates PollyJS, an HTTP recording, replaying, and stubbing tool, seamlessly into Jest and Jasmine test environments. It automatically manages the lifecycle of Polly instances for individual tests and suites, simplifying setup and teardown. The current stable version is 0.11.0, indicating it's actively maintained but pre-1.0, suggesting potential API changes. It differentiates itself by abstracting away much of the boilerplate associated with PollyJS in Jest, mirroring the convenience of built-in Mocha or QUnit PollyJS helpers. It also offers custom Jest environments (node and jsdom) to ensure compatibility with different Jest runner versions, particularly jest-circus, and handles recording name generation based on test structure.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install setup-polly-jest"],"cli":null},"imports":["import { setupPolly } from 'setup-polly-jest';","/** @jest-environment setup-polly-jest/jest-environment-node */","/** @jest-environment setup-polly-jest/jest-environment-jsdom */"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"/** @jest-environment setup-polly-jest/jest-environment-node */\n\nimport { setupPolly } from 'setup-polly-jest';\n\ndescribe('HTTP Recording with PollyJS', () => {\n  const context = setupPolly({\n    logLevel: 'info' // Example configuration option\n  });\n\n  beforeEach(() => {\n    // Intercept a specific request before the test runs\n    context.polly.server\n      .get('/api/data')\n      .intercept((req, res) => res.json({ message: 'Intercepted Data' }));\n  });\n\n  test('should be able to fetch data and use Polly', async () => {\n    context.polly.configure({ recordIfMissing: true });\n\n    // Simulate a network request (e.g., using node-fetch or similar)\n    // For a real test, you'd use your actual HTTP client (fetch, axios, etc.)\n    const response = await fetch('http://example.com/api/data');\n    const data = await response.json();\n\n    expect(response.status).toBe(200);\n    expect(data.message).toBe('Intercepted Data');\n\n    // The recording name is automatically generated based on suite/test names.\n    // Polly will stop and save the recording automatically after the test.\n  });\n\n  afterEach(() => {\n    // Optional: perform actions after Polly has done its cleanup\n    // For example, flushing pending requests if not handled by default stop()\n    context.polly.flush();\n  });\n});","lang":"javascript","description":"Demonstrates setting up PollyJS in a Jest test using `setupPolly`, configuring basic interception, and verifying recorded or intercepted HTTP responses.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}