From 60c8a720ccdc83d948495cf9b76f52b0466e9b22 Mon Sep 17 00:00:00 2001 From: Offir Golan Date: Fri, 24 Jan 2020 16:56:24 -0800 Subject: [PATCH] [@pollyjs/core] Update types for v4.0.0 (#41769) --- types/pollyjs__core/index.d.ts | 44 +++++++++-------- types/pollyjs__core/pollyjs__core-tests.ts | 48 ++++++++++++++----- .../setup-polly-jest-tests.ts | 2 +- 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/types/pollyjs__core/index.d.ts b/types/pollyjs__core/index.d.ts index 286c8b4713..3603afe810 100644 --- a/types/pollyjs__core/index.d.ts +++ b/types/pollyjs__core/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for @pollyjs/core 3.0 +// Type definitions for @pollyjs/core 4.0 // Project: https://github.com/netflix/pollyjs/tree/master/packages/@pollyjs/core // Definitions by: feinoujc // Borui Gu @@ -18,7 +18,7 @@ export const Timing: { relative(ratio: number): (ms: number) => Promise; }; -export type MatchBy = (input: T) => R; +export type MatchBy = (input: T, req: Request) => R; export type Headers = Record; export interface PollyConfig { mode?: MODE; @@ -26,7 +26,7 @@ export interface PollyConfig { adapters?: Array; adapterOptions?: { fetch?: { context?: any }; - puppeteer?: { page?: any }; + puppeteer?: { page?: any; requestResourceTypes?: string[] }; xhr?: { context?: any }; [key: string]: any; }; @@ -43,8 +43,6 @@ export interface PollyConfig { logging?: boolean; recordIfMissing?: boolean; - /** @deprecated use expiryStrategy */ - recordIfExpired?: boolean; recordFailedRequests?: boolean; expiryStrategy?: EXPIRY_STRATEGY; @@ -57,16 +55,19 @@ export interface PollyConfig { body?: boolean | MatchBy; order?: boolean; - url?: { - protocol?: boolean | MatchBy; - username?: boolean | MatchBy; - password?: boolean | MatchBy; - hostname?: boolean | MatchBy; - port?: boolean | MatchBy; - pathname?: boolean | MatchBy; - query?: boolean | MatchBy; - hash?: boolean | MatchBy; - }; + url?: + | boolean + | MatchBy + | { + protocol?: boolean | MatchBy; + username?: boolean | MatchBy; + password?: boolean | MatchBy; + hostname?: boolean | MatchBy; + port?: boolean | MatchBy; + pathname?: boolean | MatchBy; + query?: boolean | MatchBy<{ [key: string]: any }>; + hash?: boolean | MatchBy; + }; }; } export interface HTTPBase { @@ -185,12 +186,13 @@ export class Polly { adapters: Map; config: PollyConfig; - pause: () => void; - play: () => void; - replay: () => void; - record: () => void; - stop: () => Promise; - flush: () => Promise; + pause(): void; + play(): void; + replay(): void; + record(): void; + passthrough(): void; + stop(): Promise; + flush(): Promise; configure(config: PollyConfig): void; connectTo(name: string | typeof Adapter): void; disconnectFrom(name: string | typeof Adapter): void; diff --git a/types/pollyjs__core/pollyjs__core-tests.ts b/types/pollyjs__core/pollyjs__core-tests.ts index 9f2d861312..a881f094a7 100644 --- a/types/pollyjs__core/pollyjs__core-tests.ts +++ b/types/pollyjs__core/pollyjs__core-tests.ts @@ -39,6 +39,10 @@ new Polly('test recording', { fetch: { context: {}, }, + puppeteer: { + page: {}, + requestResourceTypes: ['fetch', 'xhr'], + }, foo: { bar: true, }, @@ -55,17 +59,17 @@ new Polly('test recording', { }, timing: Timing.relative(3), matchRequestsBy: { - method(method) { + method(method, _req) { return method.toLowerCase(); }, headers: 1 === 1 ? { exclude: ['X-Auth'] } - : headers => { + : (headers, _req) => { delete headers['X-Auth']; return headers; }, - body(body) { + body(body, _req) { const json = JSON.parse(body); delete json.email; @@ -73,34 +77,48 @@ new Polly('test recording', { }, url: { - protocol(protocol) { + protocol(protocol, _req) { return protocol === 'http' ? 'https:' : protocol; }, - username(username) { + username(username, _req) { return username === 'johndoe' ? 'username' : username; }, - password(password) { + password(password, _req) { return password || 'password'; }, - hostname(hostname) { + hostname(hostname, _req) { return hostname.replace('.com', '.net'); }, - port(port) { + port(port, _req) { return port > 80 ? 3000 : 433; }, - pathname(pathname) { + pathname(pathname, _req) { return pathname.replace('/api/v1', '/api'); }, - query(query) { + query(query, _req) { return { ...query, token: '' }; }, - hash(hash) { + hash(hash, _req) { return hash.replace(/token=[0-9]+/, ''); }, }, }, }); +polly.configure({ + matchRequestsBy: { + url: false, + }, +}); + +polly.configure({ + matchRequestsBy: { + url(url, _req) { + return url.replace('https', 'http'); + }, + }, +}); + function log(_: string) { // no op } @@ -110,6 +128,12 @@ async function test() { polly.pause(); polly.play(); + polly.record(); + polly.replay(); + polly.passthrough(); + await polly.flush(); + await polly.stop(); + const { server } = polly; server.get('/session').on('request', req => { req.headers['X-AUTH'] = ''; @@ -192,8 +216,6 @@ async function test() { req.removeHeaders(['Content-Type', 'Content-Length']); log(req.pathname + JSON.stringify(error)); }); - - await polly.flush(); } setupMocha(); diff --git a/types/setup-polly-jest/setup-polly-jest-tests.ts b/types/setup-polly-jest/setup-polly-jest-tests.ts index c7baa26d84..752c73cb64 100644 --- a/types/setup-polly-jest/setup-polly-jest-tests.ts +++ b/types/setup-polly-jest/setup-polly-jest-tests.ts @@ -1,7 +1,7 @@ import { setupPolly } from 'setup-polly-jest'; setupPolly(); -setupPolly({ recordIfExpired: true }); +setupPolly({ recordIfMissing: true }); setupPolly({ adapters: ['xhr'] }); const context = setupPolly();