Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Spying on property changes with Sinon

I am stubbing properties with sinon. I have trouble understanding how the related spying works. Most of the sinon spy methods seem to be related to function calls and not to access of object properties. As a workaround I created a variable for tracking the property access and set it manually when the related getter function is called. See the code example below. This seems to work just fine but I feel like I am probably duplicating some functionality that already exists in sinon. What would be a more idiomatic way of achieving the same result?

      const sinon = require('sinon');
      const lib = require('./lib');
      const app = require('./app');

      const sandbox = sinon.createSandbox();

      let fooAccessed = false;

      sandbox.stub(lib, 'foo').get(() => {
        fooAccessed = true;
        return 123
      });

      app();

      expect(fooAccessed).toEqual(true);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I know this is probably not the answer you want, because i do not use "sinon", but i will answer anyway. Perhaps this helps you in any way.

You can use a Proxy object to intercept accessing a object.

const obj = {
    foo: "bar",
    baz: true
};

let accessed = false;

const proxied = new Proxy(obj, {
    get(target, prop) {
        if (prop === "foo") {
            accessed = true;
        }
        return target[prop];
    }
});

console.log(proxied, accessed);
console.log(proxied.foo);
console.log(proxied, accessed);

Result:

{ foo: 'bar', baz: true } false
bar
{ foo: 'bar', baz: true } true
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!