{"record":{"id":"cb837d3a6ba4848f","repo":"nodejs/node","slug":"und-mock-err-mock-not-matched","errorCode":"UND_MOCK_ERR_MOCK_NOT_MATCHED","errorMessage":"Mock dispatch not matched for path '${resolvedPath}'","messagePattern":"Mock dispatch not matched for path '(.+?)'","errorType":"exception","errorClass":"MockNotMatchedError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/mock/mock-utils.js","lineNumber":186,"sourceCode":"  }\n}\n\nfunction getMockDispatch (mockDispatches, key) {\n  const basePath = key.query ? serializePathWithQuery(key.path, key.query) : key.path\n  const resolvedPath = typeof basePath === 'string' ? safeUrl(basePath) : basePath\n\n  const resolvedPathWithoutTrailingSlash = removeTrailingSlash(resolvedPath)\n\n  // Match path\n  let matchedMockDispatches = mockDispatches\n    .filter(({ consumed }) => !consumed)\n    .filter(({ path, ignoreTrailingSlash }) => {\n      return ignoreTrailingSlash\n        ? matchValue(removeTrailingSlash(safeUrl(path)), resolvedPathWithoutTrailingSlash)\n        : matchValue(safeUrl(path), resolvedPath)\n    })\n  if (matchedMockDispatches.length === 0) {\n    throw new MockNotMatchedError(`Mock dispatch not matched for path '${resolvedPath}'`)\n  }\n\n  // Match method\n  matchedMockDispatches = matchedMockDispatches.filter(({ method }) => matchValue(method, key.method))\n  if (matchedMockDispatches.length === 0) {\n    throw new MockNotMatchedError(`Mock dispatch not matched for method '${key.method}' on path '${resolvedPath}'`)\n  }\n\n  // Match body\n  matchedMockDispatches = matchedMockDispatches.filter(({ body }) => typeof body !== 'undefined' ? matchValue(body, key.body) : true)\n  if (matchedMockDispatches.length === 0) {\n    throw new MockNotMatchedError(`Mock dispatch not matched for body '${key.body}' on path '${resolvedPath}'`)\n  }\n\n  // Match headers\n  matchedMockDispatches = matchedMockDispatches.filter((mockDispatch) => matchHeaders(mockDispatch, key.headers))\n  if (matchedMockDispatches.length === 0) {\n    const headers = typeof key.headers === 'object' ? JSON.stringify(key.headers) : key.headers","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/mock/mock-utils.js#L168-L204","documentation":"Thrown by getMockDispatch when, after filtering out consumed dispatches and applying path matching, no registered mock dispatch's path matches the request's resolved path. This is the first matching stage in the mock resolution pipeline; path is matched via matchValue (string equality, RegExp test, or predicate function), with optional trailing-slash normalization.","triggerScenarios":"A request is dispatched (via client/pool/fetch under a MockAgent) whose URL path does not match any intercept() registration. Examples: requesting '/user' when only '/users' is registered; requesting '/users?id=1' when the path was registered without considering query normalization; all dispatches for that path already consumed; the mock agent is active but no intercept was added.","commonSituations":"Typo in the intercepted path; path includes query string that wasn't in the registration (use opts.query); all repeat-count consumed; trailing slash mismatch (enable ignoreTrailingSlash); forgot to disable net connect so real requests bypass mocks; the MockAgent was not enabled.","solutions":["Register an intercept for the exact path the request uses: intercept({ path: '/users' }).","If the request has query params, pass opts.query to intercept() or include them in the path.","Increase repeat count with .times(N) or .persist() if dispatches are being consumed.","Set ignoreTrailingSlash: true on the agent/interceptor if '/users' vs '/users/' is the issue.","Verify mockAgent.disableNetConnect() / enableNetConnect is set so unmatched requests fail loudly rather than hitting the network."],"exampleFix":"// before\nmockAgent.disableNetConnect()\nclient.intercept({ path: '/users', method: 'GET' }).reply(200)\nawait request('/user') // typo, no match\n\n// after\nclient.intercept({ path: '/user', method: 'GET' }).reply(200)\nawait request('/user')\n// or fix the request to '/users'","handlingStrategy":"try-catch","validationCode":"function ensureInterceptForPath(mockClient, path, method = 'GET') {\n  const dispatches = mockClient[kDispatches] || [];\n  const exists = dispatches.some(d => !d.consumed && matchPath(d.path, path));\n  if (!exists) {\n    mockClient.intercept({ path, method }).reply(200);\n  }\n}","typeGuard":"function isPathRegistered(dispatches, path) {\n  return dispatches.some(d => !d.consumed && (d.path === path || (d.path instanceof RegExp && d.path.test(path))));\n}","tryCatchPattern":"try {\n  await client.request({ path, method });\n} catch (e) {\n  if (e.code === 'UND_MOCK_ERR_MOCK_NOT_MATCHED' && /not matched for path/.test(e.message)) {\n    // register the missing intercept, then retry\n    client.intercept({ path, method }).reply(200);\n    await client.request({ path, method });\n  } else throw e;\n}","preventionTips":["Call mockAgent.disableNetConnect() in tests so unmatched requests fail loudly instead of hitting the network.","Keep a single source of truth mapping paths to intercept registrations.","Use .persist() or a high .times(N) when a path is hit repeatedly.","Set ignoreTrailingSlash when path trailing-slash variation is expected."],"tags":["undici","mock","matching","path","mock-not-matched"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}