Skip to content
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
16 changes: 16 additions & 0 deletions test/types/mock-agent.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ expectAssignable<MockAgent>(new MockAgent({}))
format(pendingInterceptors: readonly PendingInterceptor[]): string;
}
}) => void>(agent.assertNoPendingInterceptors)

// the same types have to be reachable from outside the package
expectAssignable<MockAgent.PendingInterceptor>({
origin: 'http://localhost',
path: '/',
method: 'GET',
data: { error: null, statusCode: 200, data: '', headers: {}, trailers: {} },
consumed: false,
times: null,
persist: false
})
expectAssignable<MockAgent.PendingInterceptorsFormatter>({
format (pendingInterceptors: readonly MockAgent.PendingInterceptor[]): string {
return String(pendingInterceptors.length)
}
})
}

// issue #3444
Expand Down
21 changes: 11 additions & 10 deletions types/mock-agent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { MockCallHistory } from './mock-call-history'

export default MockAgent

interface PendingInterceptor extends MockDispatch {
origin: string;
}

/** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
declare class MockAgent<TMockAgentOptions extends MockAgent.Options = MockAgent.Options> extends Dispatcher {
constructor (options?: TMockAgentOptions)
Expand Down Expand Up @@ -40,17 +36,22 @@ declare class MockAgent<TMockAgentOptions extends MockAgent.Options = MockAgent.
enableCallHistory (): this
/** Disable call history. Any subsequence calls will then not be registered. */
disableCallHistory (): this
pendingInterceptors (): PendingInterceptor[]
pendingInterceptors (): MockAgent.PendingInterceptor[]
assertNoPendingInterceptors (options?: {
pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
pendingInterceptorsFormatter?: MockAgent.PendingInterceptorsFormatter;
}): void
}

interface PendingInterceptorsFormatter {
format(pendingInterceptors: readonly PendingInterceptor[]): string;
}

declare namespace MockAgent {
/** An interceptor that was registered on a mock dispatcher and has not been consumed yet. */
export interface PendingInterceptor extends MockDispatch {
origin: string;
}

export interface PendingInterceptorsFormatter {
format(pendingInterceptors: readonly PendingInterceptor[]): string;
}

/** MockAgent options. */
export interface Options extends Agent.Options {
/** A custom agent to be encapsulated by the MockAgent. */
Expand Down