-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Describe the bug
expect.any() and expect.anything() are typed as returning any right now:
vitest/packages/expect/src/types.ts
Lines 102 to 103 in 85e6f99
| anything: () => any | |
| any: (constructor: unknown) => any |
any is generally not particularly type safe. unknown is the safer equivalent. AFAIK there's no reason why an external consumer of Vitest would want the more permissive, less safe any here instead of unknown.
Proposal: purely as a type system change, can we switch those two any types to unknown?
I sent #7016 as a draft for reference.
Reproduction
The main reason I'm filing this is because the unsafe any-ness gets picked up by @typescript-eslint/no-unsafe-assignment in some cases.
https://github.com/JoshuaKGoldberg/repros/tree/repro-vitest-expect-any-tseslint-no-unsafe-assignment
In a nutshell, these value: ... lines are the issue:
const logger = vi.fn();
expect(logger).toHaveBeenCalledWith({
value: expect.any(Object),
});
expect(logger).toHaveBeenCalledWith({
value: expect.anything(),
});Just to be thorough, here's what you get if you console.log the two in that order:
Any {
'$$typeof': Symbol(jest.asymmetricMatcher),
sample: [Function: Object],
inverse: false
}
Anything {
'$$typeof': Symbol(jest.asymmetricMatcher),
sample: undefined,
inverse: false
}
System Info
n.b. only `npmPackages` is relevant here; this is just in the type system.
System:
OS: macOS 13.2
CPU: (10) arm64 Apple M1 Max
Memory: 535.77 MB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 22.4.1 - ~/.nvm/versions/node/v22.4.1/bin/node
Yarn: 1.22.19 - ~/.yarn/bin/yarn
npm: 10.8.1 - ~/.nvm/versions/node/v22.4.1/bin/npm
pnpm: 9.12.3 - ~/.nvm/versions/node/v22.4.1/bin/pnpm
Browsers:
Chrome: 131.0.6778.86
Safari: 16.3
npmPackages:
vitest: ^2.1.8 => 2.1.8Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.