Skip to content

MockAgent MockCallHistory.filterCallsByX broken since 8.2.0 #5324

@mwebbmwnz

Description

@mwebbmwnz

Bug Description

Since 8.2.0, the filterCallsByX functions on MockCallHistory appear to be broken.

They seem to now require an additional "logs" parameter which I don't think is intended. If I add in MockCallHistory.logs (internal) as the second parameter the function works. This parameter is not documented in the documentation or the function types:

filterCallsByProtocol (protocol: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given host. if a string is given, it is matched with includes */
filterCallsByHost (host: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given port. if a string is given, it is matched with includes */
filterCallsByPort (port: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given origin. if a string is given, it is matched with includes */
filterCallsByOrigin (origin: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given path. if a string is given, it is matched with includes */
filterCallsByPath (path: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given hash. if a string is given, it is matched with includes */
filterCallsByHash (hash: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given fullUrl. if a string is given, it is matched with includes */
filterCallsByFullUrl (fullUrl: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given method. if a string is given, it is matched with includes */
filterCallsByMethod (method: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>

This might have been introduced by #5045?

Reproducible By

import { MockAgent, request } from 'undici'

const mockAgent = new MockAgent({ enableCallHistory: true })
mockAgent.disableNetConnect()

mockAgent.get('https://example.com').intercept({ method: 'GET', path: '/hello' }).reply(200, { ok: true });

// (avoiding #5036 by not using global fetch)
const response = await request('https://example.com/hello', { dispatcher: mockAgent })
console.log(`Response status: ${response.statusCode}`)


const callHistory = mockAgent.getCallHistory();
console.log(`Calls: ${callHistory.calls()}`)

// does not work on 8.2.0+
console.log(`filterCallsByPath: ${callHistory.filterCallsByPath('/hello')}`)

// logs parameter required on 8.2.0+ (MockCallHistory.logs is not public)
// console.log(`filterCallsByPath with logs: ${callHistory.filterCallsByPath('/hello', callHistory.logs)}`)

Expected Behavior

functions continue to work without the additional parameter as on <=8.1.0

Logs & Screenshots

Output from above example

8.1.0

Response status: 200
Calls: protocol->https:|host->example.com|port->|origin->https://example.com|path->/hello|hash->|searchParams->{}|fullUrl->https://example.com/hello|method->GET|body->undefined|headers->undefined
filterCallsByPath: protocol->https:|host->example.com|port->|origin->https://example.com|path->/hello|hash->|searchParams->{}|fullUrl->https://example.com/hello|method->GET|body->undefined|headers->undefined

8.2.0 and above

Response status: 200
Calls: protocol->https:|host->example.com|port->|origin->https://example.com|path->/hello|hash->|searchParams->{}|fullUrl->https://example.com/hello|method->GET|body->undefined|headers->undefined
/Users/User/WebstormProjects/undici-bug/node_modules/undici/lib/mock/mock-call-history.js:40
      return logs.filter((log) => {
                  ^

TypeError: Cannot read properties of undefined (reading 'filter')
    at MockCallHistory.filterCallsByPath (/Users/User/WebstormProjects/undici-bug/node_modules/undici/lib/mock/mock-call-history.js:40:19)
    at file:///Users/User/WebstormProjects/undici-bug/index.js:17:47

Node.js v24.14.1

Process finished with exit code 1

Environment

Additional context

node v24.14.1, MacOS 26.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions