You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No way to stub private module functions.
At least I think so as I haven't found any way to do it in the docs.
Issue
I am unit testing a module. Lets say it's moduleA.js
functionsaySecret(){// Some complicated and possibly side effects// operations which are// not important for sayHi() testreturn'Few possible options which are not important';}functionsayHi(password){letsayHiReturnedValue='some value';if(password){sayHiReturnedValue+=` ${saySecret()}`;// 👈 Calling the non-export function}returnsayHiReturnedValue;}module.exports={ sayHi };
Now the unit itself:
const{ expect }=require('chai');consttd=require('testdouble');constanything=td.matchers.anything();describe('sayHi()',function(){beforeEach(()=>{td.reset();deleterequire.cache[require.resolve('../../moduleA')];this.moduleA=require('../../moduleA');});it('Should return expected value',()=>{// ❗ What I would like to achieve: ❗constsaySecret=td.replace(`somehow replace saySecret()`);// 👈td.when(saySecret(anything)).thenReturn('controlled value');// 👈consttestedValue=this.moduleA.sayHi('something');expect(testedValue).to.equal('some value controlled value');});});
I have found some other lib which makes it possible but installing that is not an option as I would need to get unlimited amount of permissions. The lib is called rewire and all the steps how to do it are described here.
I just want to know if testdouble will give me that option. And if so how to achieve that.
Environment
node -v output: v14.18.1
npm -v (or yarn --version) output: 6.14.15
npm ls testdouble (or yarn list testdouble) version: testdouble@3.16.1
The text was updated successfully, but these errors were encountered:
This is by design. The goal of testdouble.js is to facilitate test-driven development of a given unit by faking out that unit's dependencies via public interfaces.
In this presentation I talk a bit about why mocking private APIs is discouraged.
Thanks for the quick response. I totally understand your point...unfortunately sometimes we are blocked by legacy design and changing it would cause huge problems.
It would be just nice to have that option but it would probably create a temptation to use it against best practices...
Description
No way to stub private module functions.
At least I think so as I haven't found any way to do it in the docs.
Issue
I am unit testing a module. Lets say it's moduleA.js
Now the unit itself:
I have found some other lib which makes it possible but installing that is not an option as I would need to get unlimited amount of permissions. The lib is called
rewire
and all the steps how to do it are described here.I just want to know if testdouble will give me that option. And if so how to achieve that.
Environment
node -v
output: v14.18.1npm -v
(oryarn --version
) output: 6.14.15npm ls testdouble
(oryarn list testdouble
) version: testdouble@3.16.1The text was updated successfully, but these errors were encountered: