{"record":{"id":"2d9eadd3097cc545","repo":"pestphp/pest","slug":"sequence-expectations-are-more-than-the-iterable-i","errorCode":null,"errorMessage":"Sequence expectations are more than the iterable items.","messagePattern":"Sequence expectations are more than the iterable items\\.","errorType":"exception","errorClass":"OutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Expectation.php","lineNumber":230,"sourceCode":"            throw new InvalidArgumentException('No sequence expectations defined.');\n        }\n\n        $index = $valuesCount = 0;\n\n        foreach ($this->value as $key => $value) {\n            $valuesCount++;\n\n            if ($callbacks[$index] instanceof Closure) {\n                $callbacks[$index](new self($value), new self($key));\n            } else {\n                new self($value)->toEqual($callbacks[$index]);\n            }\n\n            $index = isset($callbacks[$index + 1]) ? $index + 1 : 0;\n        }\n\n        if ($valuesCount < count($callbacks)) {\n            throw new OutOfRangeException('Sequence expectations are more than the iterable items.');\n        }\n\n        return $this;\n    }\n\n    /**\n     * @template TMatchSubject of array-key\n     *\n     * @param  (callable(): TMatchSubject)|TMatchSubject  $subject\n     * @param  array<TMatchSubject, (callable(self<TValue>): mixed)|TValue>  $expressions\n     * @return self<TValue>\n     */\n    public function match(mixed $subject, array $expressions): self\n    {\n        $subject = $subject instanceof Closure ? $subject() : $subject;\n\n        $matched = false;\n","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/pestphp/pest/blob/1af74a215c3a89bc7a44fc6f9322e369eaad2423/src/Expectation.php#L212-L248","documentation":"sequence() applies callbacks in order and wraps back to the first callback when items outnumber callbacks; the opposite direction is an error. After iterating, Pest throws OutOfRangeException if valuesCount < count($callbacks) — you declared more sequence expectations than the iterable has items (including the case where the iterable is empty and any callbacks were given).","triggerScenarios":"expect([1, 2])->sequence(fn ($v) => $v->toBe(1), fn ($v) => $v->toBe(2), fn ($v) => $v->toBe(3)); expect([])->sequence(fn ($v) => ...); a dataset/pagination result with fewer rows than the expected sequence; off-by-one when counting items.","commonSituations":"API responses that return fewer records than expected (filtered/empty results); mocks or factories configured to produce fewer entities than the test's sequence asserts; forgetting that sequence wraps — the LAST callback repeats for surplus items, so extra expectations cannot be absorbed.","solutions":["Match the counts: remove surplus sequence entries so count(callbacks) <= count(items).","If the item count is legitimately variable, rely on wrap-around: keep only as many callbacks as the guaranteed prefix and let the last one repeat.","Fix the data producer so it returns the full expected set (e.g., correct the query/mocking that dropped rows).","Assert the size first: expect($items)->toHaveCount(3) before sequence() to fail with a clearer message."],"exampleFix":"// before\nexpect([1, 2])->sequence(\n    fn ($v) => $v->toBe(1),\n    fn ($v) => $v->toBe(2),\n    fn ($v) => $v->toBe(3), // more expectations than items\n);\n// after\nexpect([1, 2])->sequence(\n    fn ($v) => $v->toBe(1),\n    fn ($v) => $v->toBe(2),\n);","handlingStrategy":"validation","validationCode":"$callbacks = [/* ... */];\n$items = [...];\nif (count($items) < count($callbacks)) {\n    throw new LogicException(\n        'sequence() needs at least as many items ('.count($items).') as expectations ('.count($callbacks).')'\n    );\n}\nexpect($items)->sequence(...$callbacks);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pair every sequence() with a preceding expect($items)->toHaveCount(n) so size regressions fail with the clearest message.","Remember wrap-around: fewer callbacks than items repeats the LAST callback — never the reverse.","When item counts vary, assert only the stable prefix in sequence() and use each() for the remainder."],"tags":["pest","expectation","sequence","count-mismatch","off-by-one"],"backgroundTag":"sequence-length-mismatch","analyzedSha":"1af74a215c3a89bc7a44fc6f9322e369eaad2423","analyzedAt":"2026-08-21T02:16:38.132Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}