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

99
Views
Unit test, how to use Sinon mock a function and assert it has been call

I am new to UnitTest. I have a setData function and trying to write test to it with mocha chai. I trying to mock the function2.setCode function that require from other module and assert it has been call when test the setData function. But have error about wrap undefined property when test. Anyone can help how to achieve that?

const function2 = require(../function2)
function setData(req) {
  if (!req) {
    return;
  }

  const data = function1(req);
  if (data) {
    const function2 = new function2(req);
    if (function2) {
      function2.setCode(data);
      );
    }
  }
}

My draft

describe('test', ()=>{
    context('setData', ()=>{
        it('function2.setCode should be call', ()=>{
            let mock = mock(setData);
            mock.expects("setCode").once();
            setData();
            mock.verify();
        })
    })
}
about 4 years ago · Juan Pablo Isaza
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!