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

97
Views
Sinon Spy on all function calls within a function

Say we have a function like this in flow.js which is a web component so all are functions inside a class within this file.

change(){
   
   this.getParams();
   this.loadData();
}

I want to write a test case that can test change() and confirm whether these two functions have been invoked. I tried many approaches but it dint work. One of the approaches I tried is:

  it('change() test', function () {
     const sandbox = sinon.createSandbox();
     sandbox.spy(el);
     el.change();
     assert(el.change.calledOnce); // output is true
     assert(el.getParams.calledOnce); // output is false
     assert(el.loadData.calledOnce); // output is false
  }

el is the object/webcomponent.

Could you please let me know what is wrong here and what is the right implementation for this scenario ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should add spy to el.getParams() and el.loadData() methods.

E.g.

flow.js:

class Flow {
  change() {
    this.getParams();
    this.loadData();
  }
  getParams() {}
  loadData() {}
}

module.exports = Flow;

flow.test.js:

const Flow = require('./flow');
const sinon = require('sinon');

describe('flow', () => {
  it('should pass', () => {
    const sandbox = sinon.createSandbox();
    const flow = new Flow();
    sandbox.spy(flow, 'getParams');
    sandbox.spy(flow, 'loadData');
    flow.change();
    sinon.assert.calledOnce(flow.getParams);
    sinon.assert.calledOnce(flow.loadData);
  });
});
about 4 years ago · Juan Pablo Isaza 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!