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?