How can I stub a function returned by a function? (TS)

34 views Asked by At

I am trying to stub a function returned by a function component but can't seem to find a correct way...

Here is what my module looks like:

export function myModule(dependencies): myModuleType {

    const logger = setupLogger(dependencies);

    const myFunction = (input: string): void => {
        logger.log('Input', input);
        return;
    }

    return {
        myFunction,
    };
}

This is called within my main.ts:

....

module = myModule(deps);

module.myFunction('Hello!');

....

In my tests, I start my main.ts, and then I want to run a test that myFunction was called once. So my question is, how can I stub myFunction such that when it is called by main.ts I can confirm that it was called?

I have tried a few things like:

import * as myModuleFile from './myModule';

myFunctionSpy = sinon.spy();

sinon.stub(myModuleFile, 'myModule').returns({
    myFunction: myFunctionSpy,
});

// ... (start main.ts)

assert.calledOnceWithExactly(myFunctionSpy, 'Hello!);

...

But the stub is not working. I would appreciate any advice, thanks!

Stub to pick up that the module function is called within my main file for integration testing

0

There are 0 answers