Skip to content

fix(expect): reject nullish toHaveProperty subjects (fix #10735)#10766

Open
mrfandu1 wants to merge 1 commit into
vitest-dev:mainfrom
mrfandu1:fix/to-have-property-nullish-10735
Open

fix(expect): reject nullish toHaveProperty subjects (fix #10735)#10766
mrfandu1 wants to merge 1 commit into
vitest-dev:mainfrom
mrfandu1:fix/to-have-property-nullish-10735

Conversation

@mrfandu1

Copy link
Copy Markdown

Description

expect(null).toHaveProperty('x') crashed with a raw internal error —
TypeError: Cannot convert undefined or null to object pointing into Vitest
internals — because getValue() called Object.hasOwn(actual, propertyName)
before the null-safe utils.getPathInfo fallback could run. The negated form
expect(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:

TypeError: .toHaveProperty() expects to receive an object, but got null

The wording matches the existing .toMatch() error style in the same file.
The .not form also throws rather than passing because asserting properties
on a nullish subject is a test error; expect(x).toBeDefined() should come
first.

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:

  • 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.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Verification performed:

  • CI=true pnpm test jest-expect.test.ts
    • 342 passed and 27 expected failures across forks, threads, and vmThreads.
  • pnpm typecheck
  • pnpm lint
  • pnpm build
  • pnpm test:ci
    • The affected suite passed. The broader local Windows run encountered unrelated coverage and workspace test failures involving coverage-directory races, timeouts, and existing snapshot differences.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

No documentation changes are needed for this bug fix.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copilot AI review requested due to automatic review settings July 13, 2026 09:55
@github-actions github-actions Bot added the maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control label Jul 13, 2026
@github-actions

Copy link
Copy Markdown

Hello @mrfandu1. Your PR has been labeled maybe automated because it appears to have been fully generated by AI with no human involvement.

To keep your PR open, please follow these steps:

  • Confirm that you are a real human. If you are an automated agent, disclose that
  • Confirm you've read, reviewed and stand behind its content
  • Confirm you've read the full issue along with all of its comments, as well as any linked issues and their comments
  • Make sure it follows our contribution guidelines and uses the correct GitHub template
  • Disclose any AI tools you used (e.g. Claude, Copilot, Codex)

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 throws TypeError: .toHaveProperty() expects to receive an object, but got ….
  • Add unit tests covering both positive and negated (.not) forms for null and undefined, 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.
@mrfandu1 mrfandu1 force-pushed the fix/to-have-property-nullish-10735 branch from c68be95 to cef95b2 Compare July 13, 2026 09:57
@mrfandu1

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expect(null | undefined).toHaveProperty() throws an internal TypeError instead of a clean matcher error

2 participants