{"record":{"id":"d4629814a35af9ba","repo":"ruvnet/ruflo","slug":"expected-to-find-json-stringify-expected-after","errorCode":null,"errorMessage":"Expected to find ${JSON.stringify(expected)} after index ${lastIndex} in array","messagePattern":"Expected to find (.+?) after index (.+?) in array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/testing/src/helpers/assertion-helpers.ts","lineNumber":385,"sourceCode":" *   { type: 'End' },\n * ]);\n */\nexport function assertPartialOrder<T>(\n  actual: T[],\n  expectedOrder: Partial<T>[]\n): void {\n  let lastIndex = -1;\n\n  for (const expected of expectedOrder) {\n    const index = actual.findIndex((item, i) =>\n      i > lastIndex &&\n      Object.entries(expected as Record<string, unknown>).every(\n        ([key, value]) => (item as Record<string, unknown>)[key] === value\n      )\n    );\n\n    if (index === -1) {\n      throw new Error(\n        `Expected to find ${JSON.stringify(expected)} after index ${lastIndex} in array`\n      );\n    }\n\n    lastIndex = index;\n  }\n}\n\n/**\n * Assert that all items in a collection pass a predicate\n *\n * @example\n * assertAllPass(results, result => result.success);\n */\nexport function assertAllPass<T>(\n  items: T[],\n  predicate: (item: T, index: number) => boolean,\n  message?: string","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/testing/src/helpers/assertion-helpers.ts#L367-L403","documentation":"assertPartialOrder() walks expectedOrder and requires each expected item to appear (matched by key/value subset with strict equality) after the index where the previous one matched. It throws naming the expected item and the index boundary as soon as one cannot be found, meaning the actual array is missing an expected event or events happened out of order.","triggerScenarios":"Events emitted in a different order than asserted because of async completion races; an expected event never emitted (swallowed error); key values of different types (string id vs number id) failing strict equality.","commonSituations":"Asserting before flushing microtasks or timers; parallel tasks with nondeterministic completion order; ids collected as numbers but asserted as strings.","solutions":["Await or flush all async work before asserting (Promise.all, runAllTimers, and similar)","Include in expectedOrder only events whose relative order is actually guaranteed; drop incidental ones","Check that key names and value types match exactly, since matching uses === on item[key]"],"exampleFix":"// before\nemitEvents(); // not awaited\nassertPartialOrder(events, [{ type: 'task.completed' }, { type: 'agent.released' }]); // throws\n\n// after\nawait emitEvents(); // wait until emissions settle\nassertPartialOrder(events, [{ type: 'task.completed' }, { type: 'agent.released' }]);","handlingStrategy":"validation","validationCode":"// Pre-check order yourself to produce a targeted failure message\nfunction isPartialOrder(actual: Record<string, unknown>[], expected: Record<string, unknown>[]): boolean {\n  let last = -1;\n  for (const exp of expected) {\n    const idx = actual.findIndex((item, i) =>\n      i > last && Object.entries(exp).every(([k, v]) => item[k] === v)\n    );\n    if (idx === -1) return false;\n    last = idx;\n  }\n  return true;\n}\n\nif (!isPartialOrder(events, expectedOrder)) {\n  console.error('actual events:', JSON.stringify(events, null, 2));\n}\nassertPartialOrder(events, expectedOrder);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Flush all promises and timers before order assertions","Assert order only between events with a causal dependency","Normalize ids and types (for example String(id)) before collecting events"],"tags":["testing","assertion","event-ordering","async"],"backgroundTag":"order-assertion-failed","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}