Skip to content

Can't stub the module (object) function in the latest update #2514

@eugenet8k

Description

@eugenet8k

Describe the bug
After #2508 was merged and the new sinon v15.0.4 is published a lot of code in our project stopped working. We can an error when trying to stub the object function in a special case. The same approach work in v15.0.2

The example code that shows the problem based on attempt to stub ChaplinJS utils object.

import Chaplin from "chaplin";
import sinon from "sinon";
import routes from "./routes";

new Chaplin.Application({ routes });

console.log("before stub", Chaplin.utils.redirectTo({ url: "/test/" }));

const sandbox = sinon.createSandbox();
sandbox.stub(Chaplin.utils, "redirectTo").callsFake(() => "stubbed");

console.log("after stub", Chaplin.utils.redirectTo({ url: "/test/" }));

To Reproduce
Open code sandbox based on v15.0.4 and see console log https://codesandbox.io/p/sandbox/sinon-module-stub-broken-15-0-4-9e1f4u

Console:

before stub true
bootstrap:27 Uncaught TypeError: Cannot redefine property: redirectTo
    at Function.defineProperty (<anonymous>)
    at wrapMethod (sinon-esm.js:4607:16)
    at Function.stub (sinon-esm.js:3803:44)
    at Sandbox.stub (sinon-esm.js:3247:39)
    at ./src/index.js (index.js:10:9)
    at __webpack_require__ (bootstrap:24:1)
    at startup:6:1
    at startup:6:1

Expected behavior
Open code sandbox based on v15.0.2 and see console log https://codesandbox.io/p/sandbox/sinon-module-stub-working-15-0-2-tipumo

Console:

before stub true
index.js:12 after stub stubbed

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