diff --git a/package.json b/package.json index 4ecfcecd059..e90a4dbfbf6 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "test:node-test": "borp -p \"test/node-test/**/*.js\"", "test:tdd": "borp --expose-gc -p \"test/*.js\"", "test:tdd:node-test": "borp -p \"test/node-test/**/*.js\" -w", - "test:typescript": "tsd && tsc test/imports/undici-import.ts --downlevelIteration --typeRoots ./types --noEmit && tsc ./types/*.d.ts --noEmit --typeRoots ./types", + "test:typescript": "tsd && tsc test/imports/undici-import.ts --typeRoots ./types --noEmit && tsc ./types/*.d.ts --noEmit --typeRoots ./types", "test:webidl": "borp -p \"test/webidl/*.js\"", "test:websocket": "borp -p \"test/websocket/*.js\"", "test:websocket:autobahn": "node test/autobahn/client.js", diff --git a/test/imports/undici-import.ts b/test/imports/undici-import.ts index f3c12270772..8be95cdd009 100644 --- a/test/imports/undici-import.ts +++ b/test/imports/undici-import.ts @@ -1,6 +1,5 @@ import { expectType } from 'tsd' -import { Dispatcher, interceptors, MockCallHistory, MockCallHistoryLog, request } from '../../' -import { kMockCallHistoryAddLog, kMockCallHistoryDeleteAll } from '../../lib/mock/mock-symbols' +import { Dispatcher, interceptors, request } from '../../' async function exampleCode () { const retry = interceptors.retry() @@ -14,22 +13,4 @@ async function exampleCode () { await request('http://localhost:3000/foo') } -function checkMockCallHistoryIterator () { - const mockCallHistory = new MockCallHistory('hello') - // @ts-ignore -- not relevant here - mockCallHistory[kMockCallHistoryAddLog]({ path: '/', origin: 'http://localhost:4000', method: 'GET' }) - // @ts-ignore -- not relevant here - mockCallHistory[kMockCallHistoryAddLog]({ path: '/endpoint', origin: 'http://localhost:4000', method: 'GET' }) - - expectType>([...mockCallHistory]) - - for (const log of mockCallHistory) { - expectType(log) - } - - // @ts-ignore -- not relevant here - MockCallHistory[kMockCallHistoryDeleteAll]() -} - exampleCode() -checkMockCallHistoryIterator() diff --git a/test/types/mock-call-history.test-d.ts b/test/types/mock-call-history.test-d.ts new file mode 100644 index 00000000000..12bc9cad857 --- /dev/null +++ b/test/types/mock-call-history.test-d.ts @@ -0,0 +1,58 @@ +import { expectType } from 'tsd' +import { MockAgent, MockCallHistory, MockCallHistoryLog } from '../..' +import { MockScope } from '../../types/mock-interceptor' + +{ + const mockAgent = new MockAgent() + expectType(mockAgent.getCallHistory()) + expectType(mockAgent.getCallHistory('hello')) + expectType(mockAgent.clearAllCallHistory()) + expectType(mockAgent.enableCallHistory()) + expectType(mockAgent.disableCallHistory()) + expectType>(mockAgent.get('http://localhost:3000').intercept({ path: '/' }).reply(200, 'hello').registerCallHistory('local-history')) +} + +{ + const mockAgent = new MockAgent() + expectType(mockAgent.getCallHistory()?.firstCall()) + expectType(mockAgent.getCallHistory()?.lastCall()) + expectType(mockAgent.getCallHistory()?.nthCall(1)) + expectType | undefined>(mockAgent.getCallHistory()?.calls()) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByFullUrl('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByHash('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByHost('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByMethod('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByOrigin('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByPath('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByPort('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCallsByProtocol('')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCalls((log) => log.path === '/')) + expectType | undefined>(mockAgent.getCallHistory()?.filterCalls(/path->\//)) + expectType | undefined>(mockAgent.getCallHistory()?.filterCalls({ method: 'POST' })) + expectType | undefined>(mockAgent.getCallHistory()?.filterCalls({ method: 'POST' }, { operator: 'AND' })) + + const callHistory = mockAgent.getCallHistory() + + if (callHistory !== undefined) { + expectType>([...callHistory]) + expectType>(new Set(callHistory)) + + for (const log of callHistory) { + expectType(log) + expectType(log.body) + expectType(log.fullUrl) + expectType(log.hash) + expectType> | null | undefined>(log.headers) + expectType(log.host) + expectType(log.method) + expectType(log.origin) + expectType(log.path) + expectType(log.port) + expectType(log.protocol) + expectType>(log.searchParams) + expectType | null | undefined>>(log.toMap()) + expectType(log.toString()) + } + } + expectType(mockAgent.getCallHistory()?.clear()) +} diff --git a/types/mock-call-history.d.ts b/types/mock-call-history.d.ts index 9d14e530f70..df07fa0dca0 100644 --- a/types/mock-call-history.d.ts +++ b/types/mock-call-history.d.ts @@ -8,9 +8,9 @@ declare namespace MockCallHistoryLog { /** a log reflecting request configuration */ declare class MockCallHistoryLog { constructor (requestInit: Dispatcher.DispatchOptions) - /** protocol used. */ + /** protocol used. ie. 'https:' or 'http:' etc... */ protocol: string - /** request's host. ie. 'https:' or 'http:' etc... */ + /** request's host. */ host: string /** request's port. */ port: string @@ -23,22 +23,29 @@ declare class MockCallHistoryLog { /** the full url requested. */ fullUrl: string /** request's method. */ - method: Dispatcher.DispatchOptions['method'] + method: string /** search params. */ searchParams: Record /** request's body */ - body: Dispatcher.DispatchOptions['body'] + body: string | null | undefined /** request's headers */ - headers: Dispatcher.DispatchOptions['headers'] + headers: Record | null | undefined /** returns an Map of property / value pair */ - toMap (): Map + toMap (): Map | null | undefined> /** returns a string computed with all key value pair */ toString (): string } declare namespace MockCallHistory { + export type FilterCallsOperator = 'AND' | 'OR' + + /** modify the filtering behavior */ + export interface FilterCallsOptions { + /** the operator to apply when filtering. 'OR' will adds any MockCallHistoryLog matching any criteria given. 'AND' will adds only MockCallHistoryLog matching every criteria given. (default 'OR') */ + operator?: FilterCallsOperator | Lowercase + } /** a function to be executed for filtering MockCallHistoryLog */ export type FilterCallsFunctionCriteria = (log: MockCallHistoryLog) => boolean @@ -47,13 +54,21 @@ declare namespace MockCallHistory { /** an object to execute multiple filtering at once */ export interface FilterCallsObjectCriteria extends Record { + /** filter by request protocol. ie https: */ protocol?: FilterCallsParameter; + /** filter by request host. */ host?: FilterCallsParameter; + /** filter by request port. */ port?: FilterCallsParameter; + /** filter by request origin. */ origin?: FilterCallsParameter; + /** filter by request path. */ path?: FilterCallsParameter; + /** filter by request hash. */ hash?: FilterCallsParameter; + /** filter by request fullUrl. */ fullUrl?: FilterCallsParameter; + /** filter by request method. */ method?: FilterCallsParameter; } } @@ -69,8 +84,8 @@ declare class MockCallHistory { lastCall (): MockCallHistoryLog | undefined /** returns the nth MockCallHistoryLog. */ nthCall (position: number): MockCallHistoryLog | undefined - /** return all MockCallHistoryLog matching any of criteria given. */ - filterCalls (criteria: MockCallHistory.FilterCallsObjectCriteria | MockCallHistory.FilterCallsFunctionCriteria | RegExp): Array + /** return all MockCallHistoryLog matching any of criteria given. if an object is used with multiple properties, you can change the operator to apply during filtering on options */ + filterCalls (criteria: MockCallHistory.FilterCallsFunctionCriteria | MockCallHistory.FilterCallsObjectCriteria | RegExp, options?: MockCallHistory.FilterCallsOptions): Array /** return all MockCallHistoryLog matching the given protocol. if a string is given, it is matched with includes */ filterCallsByProtocol (protocol: MockCallHistory.FilterCallsParameter): Array /** return all MockCallHistoryLog matching the given host. if a string is given, it is matched with includes */