Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Extend TypeScript example #464

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions examples/typescript-jest-node-fetch/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
54 changes: 49 additions & 5 deletions examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,13 +79,14 @@ export default function autoSetupPolly() {
mode,
recordIfMissing,
flushRequestsOnStop: true,
logging: false,
logLevel: "warn",
recordFailedRequests: true,
persister: "fs",
persisterOptions: {
fs: {
recordingsDir: path.resolve(__dirname, "../../__recordings__"),
},
},
...overrideConfig,
});
}