vendredi 4 août 2017

Jest test module while mocking function from same module

I am trying to verify that a function within a module calls another function within that module. When I try the following, Jest reports that bar was called 0 times. How can I test the function call successfully?

// module.js

function foo() {
    bar()
}

function bar() {
    ...
}

export {foo, bar}

// __tests__/module-test.js

import * as module from "../module";

test("foo calls bar", () => {
    module.bar = jest.fn();
    module.foo();
    expect(module.bar).toHaveBeenCalledTimes(1)
})

Aucun commentaire:

Enregistrer un commentaire