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:
- It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
- Ideally, include a test that fails without this PR but passes with it.
- Please, don't make changes to
pnpm-lock.yamlunless you introduce a new test example. - Please check Allow edits by maintainers to make review process faster.
Tests
- Run the tests with
pnpm test:ci.
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
- If you introduce new functionality, document it.
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
- Changes in changelog are generated from PR name.
AI disclosure: this PR was prepared with the assistance of Claude (Claude Code).