fix(expect): reject nullish toHaveProperty subjects (fix #10735)#10766
fix(expect): reject nullish toHaveProperty subjects (fix #10735)#10766mrfandu1 wants to merge 1 commit into
Conversation
|
Hello @mrfandu1. Your PR has been labeled To keep your PR open, please follow these steps:
Please, do not generate or format the response with AI. If you do not speak English, reply in your native language or use translation software like Google Translate or Deepl. If the response is generated, the PR will be closed automatically. These measures help us reduce maintenance burden and keep the team's work efficient. See our AI contributions policy for more context. |
There was a problem hiding this comment.
Pull request overview
Fixes Vitest’s Jest-compat .toHaveProperty() matcher to handle null/undefined subjects by throwing a clear, user-facing matcher error instead of bubbling an internal Object.hasOwn TypeError.
Changes:
- Add an explicit nullish guard in
.toHaveProperty()that throwsTypeError: .toHaveProperty() expects to receive an object, but got …. - Add unit tests covering both positive and negated (
.not) forms fornullandundefined, including the “expected value” overload.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/unit/test/jest-expect.test.ts | Adds regression tests ensuring nullish .toHaveProperty() usage throws a clear matcher error (incl. .not). |
| packages/expect/src/jest-expect.ts | Introduces a nullish subject guard in toHaveProperty to prevent internal crashes and provide a consistent TypeError message. |
Reject nullish receivers before Object.hasOwn so toHaveProperty reports a matcher-specific error for positive and negated assertions. Add regression coverage for null and undefined subjects.
c68be95 to
cef95b2
Compare
|
This is my account and I personally reviewed all the code and tests here before opening the PR. I’ve read through #10735 and the discussion, the earlier related PRs, and the contribution guidelines. I did use Claude Code to help with the fix, but I stand behind everything in it. |
Description
expect(null).toHaveProperty('x')crashed with a raw internal error —TypeError: Cannot convert undefined or null to objectpointing into Vitestinternals — because
getValue()calledObject.hasOwn(actual, propertyName)before the null-safe
utils.getPathInfofallback could run. The negated formexpect(null).not.toHaveProperty('x')crashed the same way.Per @sheremet-va's guidance in the issue, both forms now throw a clear matcher
error instead:
The wording matches the existing
.toMatch()error style in the same file.The
.notform also throws rather than passing because asserting propertieson a nullish subject is a test error;
expect(x).toBeDefined()should comefirst.
Resolves #10735
Disclosure: I used Claude Code to help develop this fix. I reviewed the
change and ran the listed checks before submitting.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Verification performed:
CI=true pnpm test jest-expect.test.tspnpm typecheckpnpm lintpnpm buildpnpm test:ciDocumentation
pnpm run docscommand.No documentation changes are needed for this bug fix.
Changesets
feat:,fix:,perf:,docs:, orchore:.