vendredi 16 février 2018

How can I test that a function calls another function internally?

I have some code like this;

// Service.js
function x() {}

function y(){
    x();
}

export default {
    x,
    y
}

And I want to use jest to test that function y calls function x internally.

I have tried

import Service from './Service';

jest.mock('./Service', () => ({
    x: jest.fn()
}));

it('should call x internally', () => {
    y();

    expect(Service.x).toBeCalled();
});

and

import Service from './Service';

it('should call x internally', () => {
    Service.x = jest.fn();

    y();

    expect(Service.x).toBeCalled();
});

But neither work. I suspect it's something like y has it's own version of x held internally, and isn't using the "global" x from the Service.js.

Can anyone help on how can I test that y calls x internally?

Aucun commentaire:

Enregistrer un commentaire