简体   繁体   English

Jest spyOn函数不是Class或Object类型

[英]Jest spyOn a function not Class or Object type

I am familiar with setting spies on Class or Object methods, but what about when the function is just an export default - such that the method itself is independent, like a utility? 我熟悉在Class或Object方法上设置间谍,但是当函数只是一个导出默认值时 - 这样方法本身是独立的,就像实用程序一样?

I have some existing code like so: 我有一些像这样的现有代码:

const Funct1 = props => {
  if(props){
    Funct2(args);
  }
  // or return something
};
const Funct2 = props => {
  // do something
  return true
};
export default Funct1;  //Yes the existing export is named the same as the "entry" method above.

And, for example, I'd like to spy on Funct1 getting called and Funct2 returns true. 而且,例如,我想监视Funct1被调用而Funct2返回true。

import Funct1 from "../../../src/components/Funct1";
describe("Test the Thing", () => {
    it("New Test", () => {
        let props = {
            active: true,
            agentStatus: "online"
        };
        const spy = spyOn(Funct2, "method name"); <-- how doe this work if not an obj or class?

        Funct1(props);
        //If I try Funct2(props) instead, terminal output is "Funct2 is not defined"

        expect(spy).toHaveBeenCalledWith(props);
    });
});

I am not expert in jest, but my recommendation to think about: 我不是开玩笑的专家,但我的建议是考虑:

1) When the function is exported as default I use something like: 1)当函数导出为默认值时,我使用如下内容:

import Funct1 from "../../../src/components/Funct1";
...
jest.mock("../../../src/components/Funct1");
...
expect(Funct1).toHaveBeenCalledWith(params);

2) When the module (utils.js) has multiple exports as 2)当模块(utils.js)有多个导出为

export const f1 = () => {};
...
export const f8 = () => {};

You can try 你可以试试

import * as Utils from "../../../src/components/utils"
const f8Spy = jest.spyOn(Utils, 'f8');
...
expect(f8Spy).toHaveBeenCalledWith(params);

Similar discussion here 这里类似的讨论

I believe that is not possible to test Funct1 calling Funct2 without modifying the existing code. 我相信无法在不修改现有代码的情况下测试Funct1调用Funct2。 If the latter is an option, here are two options to introduce dependency injection: 如果后者是一个选项,这里有两个引入依赖注入的选项:

  • Create and export a factory function: 创建和导出工厂功能:

     const Funct2 = props => { // do something return true; }; const Funct1 = CreateFunct1(Funct2); export function CreateFunct1(Funct2) { return props => { if (props) { Funct2(props); } // or return something }; } export default Funct1; // and here is the test: describe('Test the Thing', () => { it('New Test', () => { // Arrange const funct2Spy = jasmine.createSpy('Funct2'); const funct1 = CreateFunct1(funct2Spy); const props = "some data"; // Act funct1(props); // Assert expect(funct2Spy).toHaveBeenCalledWith(props); }); }); 
  • Export Funct2 too. 导出Funct2也是。 Here is a thread on this topic. 这里是一个线程在这个题目。 Maybe its example would have to be tweaked a little bit for your scenario because of the export syntax. 由于导出语法,也许它的示例必须针对您的场景稍微调整一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM