Spying on an imported function that calls another function in Jest

后端 未结 2 1207
野的像风
野的像风 2020-11-27 08:44

I\'m trying to spy on a function that\'s called by another function, both of which reside in an external file and imported.

Funcs.spec.js:

import * a         


        
相关标签:
2条回答
  • 2020-11-27 09:06

    The problem you describe is referenced on a jest issue.

    A possible solution to your problem (if you want to keep the functions inside the same file) is to use CommonJS, consider the following example:

    fns.js

    exports.funcA = () => {
      exports.funcB();
    };
    exports.funcB = () => {};
    

    fns.spec.js

    const fns = require("./fns");
    
    describe("funcA", () => {
      it("calls funcB", () => {
        fns.funcB = jest.fn();
        fns.funcA();
        expect(fns.funcB).toBeCalled();
      });
    });
    
    0 讨论(0)
  • 2020-11-27 09:14

    Only methods can be spied. There is no way to spy on funcB if it's called directly like funcB() within same module.

    In order for exported function to be spied or mocked, funcA and funcB should reside in different modules.

    This allows to spy on funcB in transpiled ES module (module object is read-only in native ESM):

    import { funcB } from './b';
    
    export const funcA = () => {
        funcB()
    }
    

    Due to that module imports are representations of modules, this is transpiled to:

    var _b = require('./b');
    
    var funcA = exports.funcA = function funcA() {
        (0, _b.funcB)();
    };
    

    Where funcB method is tied to _b module object, so it's possible to spy on it.

    0 讨论(0)
提交回复
热议问题