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

212
Views
Unit test private async method

You can test sync private functions using sinon spies:

// app
window.App = {
  const childFunc = function(){
    console.log("in childFunc")
  }

  const parentFunc = function(){
    this.childFunc()
  }

  const publicFuncs = function(){
      ...
      // Make private functions available foe testing
     _private = { 
        parentFunc: parentFunc,
        childFunc: childFunc
     }
  }

  return publicFuncs
}()

// tests
    const sandbox = sinon.createSandbox();
    const app = new window.App();

    beforeEach(function () {
      sandbox.spy(app._private);
    });

    afterEach(function () {
      sandbox.restore();
    });

    const app = new window.App();
    app._private.parentFunc()
    assert(app._private.childFunc.calledOnce)


What you're actually testing is that the key childFunc on app._private was called.

However, if parentFunc is called asyncronously, i.e.

  const otherFunc = async function(){
     await parentFunc()
  }  

Then the this keyword may not be defined inside parentFunc when it is run, which means that you cannot use this.childFunc.

You can call childFunc() directly, however sinon will not be able to detect that childFunc has been run, so you cannot test for it.

Am I missing anything here?

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!