3

Below there is a simplified version of the static class I want to test using Chai, Sinon and Mocha. It exposes 2 functions and has another internal.

//demo.js
const parentFunc = (baseNum) => {
    return childFunc(baseNum, 10);
};

const childFunc = (base, extra) => {
    const sum = base + extra;
    return internalFunc(sum);
};

const internalFunc = (num) => {
    return 100 + num;
};

module.exports = {
    parentFunc: parentFunc,
    childFunc: childFunc
}

Logic is irrelevant, what I want to know is how to spy, stub or mock all the functions of the class to have full UT coverage.

Below there are test cases I want do.

import DemoUtils from '../../src/scripts/common/demo';
import sinon from 'sinon';
import chai from 'chai';

const assert = chai.assert;
const expect = chai.expect;

describe('Demo', () => {
    describe('Internal Function', () => {
        //const res = DemoUtils.internalFunc(8);
    });

    describe('Child Function', () => {
        it('should return ', () => {
            const res = DemoUtils.childFunc(5,10);
            assert.equal(res, 115);
        });

    });

    describe('Parent Function', () => {
        it('should return 140', () => {
            const res = DemoUtils.parentFunc(30);
            assert.equal(res, 140);
        });

        it('should call the child function', () => {
            const stubFunction = sinon.stub(DemoUtils, 'childFunc');
            stubFunction.returns(13);

            const res = DemoUtils.parentFunc(30);
            assert.equal(res, 13);

            assert.equal(stubFunction.calledOnce, true);
            stubFunction.restore();
        });
    });
});
  • Internal Function: I guess internal function can't be tested because couldn't be called/mocked, isn't it?
  • Child Function: test works.
  • Parent Function: first test work but stub function never get called. I tried with spy and mocked too but I can't make it work either.

Anyone has been able to test a ES6 Static Class?

Thanks :)

  • `should call the child function` tests the internals. Unit tests are supposed to check the outcome. You wouldn't test `internalFunc` either. – a better oliver May 10 '17 at 08:11
  • Thanks @zeroflagL , I agree with Internal Functions but the reason behind mock a child function is to remove dependecies as that child function will be tested separately and the responsability for that logic will be already handled. Knowing that child function has been called with the right parameters should be enough for the parent function. – Monkey Hood May 10 '17 at 08:28
  • If one day you change `parentFunc` so that it no longer calls `childFunc`, then you would have to write a new unit test. And that defeats the point of unit tests. – a better oliver May 10 '17 at 08:33
  • @zeroflagL True but on the other hand if something breaks in child function will make fail the parent function when in the issue is only in child function. Anyway I just wanted to find out if there is an option to test those scenarions. Thanks :) – Monkey Hood May 10 '17 at 08:53
  • Let's try another point of view. _"but stub function never get called."_ Why should it be called? There is no code invoking `stubFunction`. You only have code invoking `childFunc`. But if the signature of `parentFunc` was `(baseNum, childFunc) => {` then you could pass in `stubFunction` and it would be invoked. And the fact that some other function is invoked would also no longer be an internal detail, therefore it could somewhat make sense to test that. And you can easily test it then. – a better oliver May 10 '17 at 10:18

0 Answers0