Skip to content

stub() throws when Object.prototype has a non-writable property #2702

@DimaIT

Description

@DimaIT

Describe the bug

sinon.stub() throws TypeError: Cannot assign to read only property '<key>' when any non-writable, non-configurable string-keyed own property exists on Object.prototype.

Starting in v21.1.0

Related: #2695 (different trigger via --disable-proto=throw).

To Reproduce

  1. Install sinon: npm i sinon@21.1.2
  2. Save:
'use strict';
Object.defineProperty(Object.prototype, 'someFlag', {
    value: 'x',
    writable: false,
    configurable: false,
    enumerable: false,
});
const sinon = require('sinon');
sinon.stub();
  1. Run with Node.

Expected behavior
sinon.stub() returns a stub without throwing (behavior in v21.0.1 and earlier).

Actual output

TypeError: Cannot assign to read only property 'someFlag' of object '[object Object]'
    at .../sinon/lib/sinon/util/core/walk.js:38:21
    at Array.forEach (<anonymous>)
    at walkInternal (.../sinon/lib/sinon/util/core/walk.js:36:5)
    at walkInternal (.../sinon/lib/sinon/util/core/walk.js:49:9)
    at walkInternal (.../sinon/lib/sinon/util/core/walk.js:49:9)
    at walk (.../sinon/lib/sinon/util/core/walk.js:66:12)
    at collectOwnMethods (.../sinon/lib/sinon/collect-own-methods.js:39:5)
    at addReturnedMethodsToCollection (.../sinon/lib/sinon/sandbox.js:201:21)
    at Sandbox.stub (.../sinon/lib/sinon/sandbox.js:234:9)

Context (please complete the following information):

  • Sinon version: 21.1.2 (also affects 21.1.0, 21.1.1; not affected: 21.0.1)
  • Runtime: Node.js 22.22.0
  • Output of npx envinfo --browsers --binaries:
Binaries:
  Node: 22.22.0 - /usr/bin/node
  npm: 10.9.4 - /usr/bin/npm
Browsers:
  Chrome: 147.0.7727.101
  Firefox: 149.0.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions