Skip to content

Fix #1741: record calls on stubbed getters/setters - #2733

Merged
fatso83 merged 2 commits into
sinonjs:mainfrom
youdie006:feat/accessor-call-recording
Oct 5, 2026
Merged

fatso83 merged 2 commits into
sinonjs:mainfrom
youdie006:feat/accessor-call-recording

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

Purpose (TL;DR)

stub.get(fn) / stub.set(fn) installed the raw replacement function directly as the property accessor, so invoking a stubbed getter/setter ran untracked — callCount never incremented (#1741). This wraps the replacement in a spy and exposes it as stub.getter / stub.setter, so accessor invocations are recorded (mirroring the existing spy.get / spy.set surface). This is the scoped "tiny PR" @fatso83 green-lit in the issue thread.

How to verify

const obj = { get prop() { return 1; } };
const s = sinon.stub(obj, "prop").get(() => 42);
obj.prop; obj.prop;
s.getter.callCount; // 2  (previously: not recorded)

npx mocha test/src/stub-test.js --grep "records calls to the stubbed" → 2 passing.

Checklist

  • Lint passes (eslint --max-warnings 0; prettier clean)
  • Scoped + non-breaking (+19 lines in default-behaviors.js, no API removed); full suite 1554 passing / 0 failing locally.

Fixes #1741

@mroderick

Copy link
Copy Markdown
Member

This is a really clean fix. Thank you for picking it up, and for keeping it scoped — the diff is easy to follow and exactly addresses the issue.

A couple of nice things I noticed 👏

  • Wrapping in spy() is the right approach — routes through the existing call-recording machinery without reinventing anything.
  • this context is properly preserved through the spy proxy.
  • No circular dependency introduced (spy.js doesn't import default-behaviors.js).
  • The naming collision with stub.get/stub.set makes .getter/.setter the right call.

One small suggestion: resetBehavior in stub.js doesn't clean up .getter/.setter, so after a resetBehavior() the stub still holds stale spy references. It's a narrow window (calling .get() again overwrites it), but adding delete this.getter; delete this.setter; there would keep it clean.

Nice work on this!

youdie006 and others added 2 commits October 5, 2026 21:05
stub.get(fn)/stub.set(fn) installed the raw replacement function as the property
accessor, so invoking the stubbed getter/setter ran untracked and never
incremented callCount. Wrap the replacement in a spy and expose it as
stub.getter/stub.setter so accessor invocations are recorded, mirroring the
spy.get/spy.set surface. Scoped and non-breaking.

Co-authored-by: Carl Erik Kopseng <carlerik@gmail.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a10d17-476c-73a6-8581-e0e0fa9a27f3
Co-authored-by: Carl Erik Kopseng <carlerik@gmail.com>
@fatso83
fatso83 force-pushed the feat/accessor-call-recording branch from 5f001bc to 3fc115e Compare October 5, 2026 21:06
@codecov

codecov Bot commented Oct 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.46%. Comparing base (8c663ed) to head (3fc115e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2733   +/-   ##
=======================================
  Coverage   97.45%   97.46%           
=======================================
  Files          39       39           
  Lines        1846     1852    +6     
=======================================
+ Hits         1799     1805    +6     
  Misses         47       47           
Flag Coverage Δ
unit 97.46% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fatso83

fatso83 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

I fixed the PR per Morgan's instructions. merging on checks turning green.

@fatso83
fatso83 merged commit 002ee82 into sinonjs:main Oct 5, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot spy on property getters, and stubbed getters do not record calls

4 participants