diff --git a/examples/typescript-jest-node-fetch/package.json b/examples/typescript-jest-node-fetch/package.json index 9533c9ce..21a7b41b 100644 --- a/examples/typescript-jest-node-fetch/package.json +++ b/examples/typescript-jest-node-fetch/package.json @@ -1,13 +1,14 @@ { "name": "typescript-jest-node-fetch", - "version": "1.0.0", + "version": "1.0.1", "private": true, "main": "./dist/index.js", "type": "commonjs", "exports": "./dist/index.js", "scripts": { "test": "jest --runInBand", - "test:record": "POLLY_MODE=record jest --runInBand --verbose" + "test:record": "POLLY_MODE=record jest --runInBand --verbose", + "test:offline": "POLLY_MODE=offline jest --runInBand --verbose" }, "keywords": [ "pollyjs", @@ -23,25 +24,18 @@ "node-fetch": "^2.6.6" }, "devDependencies": { - "@pollyjs/adapter-fetch": "^5.1.1", - "@pollyjs/adapter-node-http": "^5.1.1", - "@pollyjs/core": "^5.1.1", - "@pollyjs/node-server": "^5.1.1", - "@pollyjs/persister-fs": "^5.1.1", + "@pollyjs/adapter-fetch": "*", + "@pollyjs/adapter-node-http": "*", + "@pollyjs/core": "*", + "@pollyjs/node-server": "*", + "@pollyjs/persister-fs": "*", "@types/jest": "^26.0.0", "@types/node": "^16.11.11", "@types/node-fetch": "^2.5.12", - "@types/pollyjs__adapter": "^4.3.1", - "@types/pollyjs__adapter-fetch": "^2.0.1", - "@types/pollyjs__adapter-node-http": "^2.0.1", - "@types/pollyjs__core": "^4.3.3", - "@types/pollyjs__persister": "^4.3.1", - "@types/pollyjs__persister-fs": "^2.0.1", - "@types/pollyjs__utils": "^2.6.1", "@types/setup-polly-jest": "^0.5.1", "jest": "^26.6.0", "nodemon": "^2.0.15", - "setup-polly-jest": "^0.10.0", + "setup-polly-jest": "^0.11.0", "ts-jest": "^26.5.6", "ts-node": "^10.4.0", "typescript": "^4.5.2" diff --git a/examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts b/examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts index debc02bc..f7c438a7 100644 --- a/examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts +++ b/examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts @@ -23,11 +23,54 @@ switch (process.env.POLLY_MODE) { break; } -export default function autoSetupPolly() { - /** +/** + * + * ### Example: Ignoring headers (API Keys) + * + * ```ts + * import autoSetupPolly from '../utils/auto-setup-polly'; + * + * describe('Group of tests', () => { + * const polly = autoSetupPolly({ + * matchRequestsBy: { + * headers: { exclude: ['x-request-id', 'x-api-key'] }, + * } + * }); + * + * it('Api Request', async () => { + * // Polly will skip matching on the headers `x-request-id` and `x-api-key` in requests, + * // AND prevent the values being recorded! + * // ... test goes here ... + * }) + * }); + * ``` + * + * ### Example: Ignoring generated data using a callback + * + * ```ts + * + * import autoSetupPolly from '../utils/auto-setup-polly'; + * + * const polly = autoSetupPolly({ + * matchRequestsBy: { + * body(body, req) { + * const json = JSON.parse(body); + * + * delete json.uuid; + * delete json.createdDate; + * + * return JSON.stringify(json); + * } + * }); + * + * ``` + * + * @param overrideConfig + * @returns + */ +export default function autoSetupPolly(overrideConfig: PollyConfig = {}) { + /* * This persister can be adapted for both Node.js and Browser environments. - * - * TODO: Customize your config. */ return setupPolly({ // 🟡 Note: In node, most `fetch` like libraries use the http/https modules. @@ -36,7 +79,7 @@ export default function autoSetupPolly() { mode, recordIfMissing, flushRequestsOnStop: true, - logging: false, + logLevel: "warn", recordFailedRequests: true, persister: "fs", persisterOptions: { @@ -44,5 +87,6 @@ export default function autoSetupPolly() { recordingsDir: path.resolve(__dirname, "../../__recordings__"), }, }, + ...overrideConfig, }); }