fix(expect): allow readonly arrays and sets in toBeOneOf (fix #10264) by YBJ0000 · Pull Request #10374 · vitest-dev/vitest (original) (raw)

Description

expect.toBeOneOf typed its sample argument as Array<T> | Set<T>. Since mutable Array/Set are not assignable from their readonly counterparts, passing a ReadonlyArray<T>, a ReadonlySet<T>, or an as const array produced a type error — even though the runtime implementation only reads the argument and never mutates it.

This widens the public type of toBeOneOf (used by both expect(value).toBeOneOf(...) and the asymmetric expect.toBeOneOf(...)) to ReadonlyArray<T> | ReadonlySet<T>. Mutable arrays and sets are still accepted, and the runtime behaviour is unchanged.

Resolves #10264

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

A type test was added to test/unit/test/expect.test-d.ts covering as const arrays, ReadonlyArray, and ReadonlySet. It fails the typecheck without this change and passes with it. The existing runtime toBeOneOf() tests in jest-expect.test.ts still pass.

Documentation

No documentation change needed — this only relaxes a type and adds no new functionality. The docs already describe toBeOneOf as accepting an array or set.

Changesets


AI disclosure: this PR was prepared with the assistance of Claude (Claude Code).