Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to stub private module functions with test double #476

Closed
3 tasks done
kapalkat opened this issue Nov 26, 2021 · 2 comments
Closed
3 tasks done

No way to stub private module functions with test double #476

kapalkat opened this issue Nov 26, 2021 · 2 comments

Comments

@kapalkat
Copy link

kapalkat commented Nov 26, 2021

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

function saySecret() {
  // Some complicated and possibly side effects
  // operations which are
  // not important for sayHi() test
  return 'Few possible options which are not important';
}

function sayHi(password) {
  let sayHiReturnedValue = 'some value';
  if (password) {
    sayHiReturnedValue += ` ${saySecret()}`; // 👈 Calling the non-export function
  }
  return sayHiReturnedValue;
}

module.exports = { sayHi };

Now the unit itself:

const { expect } = require('chai');
const td = require('testdouble');

const anything = td.matchers.anything();

describe('sayHi()', function () {
  beforeEach(() => {
    td.reset();
    delete require.cache[require.resolve('../../moduleA')];

    this.moduleA = require('../../moduleA');
  });

  it('Should return expected value', () => {
    // ❗ What I would like to achieve: ❗
    const saySecret = td.replace(`somehow replace saySecret()`); // 👈
    td.when(saySecret(anything)).thenReturn('controlled value'); // 👈

    const testedValue = 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
@searls
Copy link
Member

searls commented Nov 29, 2021

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.

@searls searls closed this as completed Nov 29, 2021
@kapalkat
Copy link
Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants