Skip to content

Runtime crashes on JSC: Object.hasOwn is not a function #52

@bheemreddy-samsara

Description

@bheemreddy-samsara

Description

When running harness tests on React Native apps using JSC (JavaScriptCore) runtime, the runtime crashes immediately with:

Object.hasOwn is not a function. (In 'Object.hasOwn(globalThis, MATCHERS_OBJECT)', 'Object.hasOwn' is undefined)

Environment

  • react-native-harness: 1.0.0-alpha.20
  • React Native: 0.76.x
  • JS Engine: JSC (JavaScriptCore)
  • Platform: iOS / Android with Hermes disabled

Root Cause

@vitest/expect v4.x uses Object.hasOwn (ES2022) which JSC doesn't support:

// @vitest/expect/dist/index.js:674
if (!Object.hasOwn(globalThis, MATCHERS_OBJECT)) {

Note: This was previously misattributed to Hermes in #51, but Hermes has supported Object.hasOwn since recent versions. The issue specifically affects JSC runtime.

Proposed Solution

Add an Object.hasOwn polyfill to the runtime package that loads before @vitest/expect:

if (typeof Object.hasOwn !== 'function') {
  Object.hasOwn = (obj: object, prop: PropertyKey): boolean =>
    Object.prototype.hasOwnProperty.call(obj, prop);
}

Workaround

Consumers can add the polyfill themselves via Metro's getPolyfills, but this should be handled in the harness since it targets React Native environments that may use JSC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions