1

I am unit testing my JS file which consists of a lot of exported consts, with one of the functions calling some of the others held within the file, however I can't seem to get my test to see that it has done these calls - even though running the test with logging in the function to be called is triggered...

What am I doing wrong here? I found some articles/questions and have tried what they suggest but it doesn't seem to be helping...

MyFile.js

export func1 = (arg1) => {
    console.log('inside func1');
    // Do something
}
// More funcs here

export mainFunc = (mainArg) => {
    func1(mainArg);

    // More stuff with other funcs
}

test.spec.js

import * as utils from './MyFile.js';

it('Should trigger func1', () => {
    const mySpy = jest.spyOn(utils, 'func1');

    utils.mainFun('something');

    expect(mySpy).toHaveBeenCalled();
    expect(mySpy).toHaveBeenCalledWith('something');
});

When I run this test I will see inside func1 logged out in my console, but I will also see that my test fails:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1

Received number of calls: 0

physicsboy
  • 5,656
  • 17
  • 70
  • 119

0 Answers0