尝试访问mockedFunction.mock.instaces属性在Jest中给出了未定义 [英] Trying to access mockedFunction.mock.instaces property gives undefined in Jest

查看:61
本文介绍了尝试访问mockedFunction.mock.instaces属性在Jest中给出了未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟一个名为Dog的构造函数

I want to mock a constructor function named Dog

Dog = jest.fn(()=>{
    return{
        name:"spike",
        bark:function(){
            return "bhow " +this.name;
        }
    }
})

function foo(){
   const d = new Dog();
   return d.bark();
}




test("testing foo",()=>{
    const result = foo();
    expect(Dog).toHaveBeenCalledTimes(1);
    expect(result).toBe("bhow spike");

    expect(Dog.mock.instances.length).toBe(1);

    expect(Dog.mock.instances[0].name).toBe("spike");
    //this test failed with expected spike received undefined
});

但是 expect(Dog.mock.instances [0] .name).toBe("spike"); 失败,收到未定义的预期峰值

but the expect(Dog.mock.instances[0].name).toBe("spike"); fails with expected spike received undefined

最佳版本24.8.0节点版本10.15.0

jest version 24.8.0 node version 10.15.0

推荐答案

使用 new 运算符调用函数时,将创建一个新对象并将其作为执行上下文(也称为)添加到函数中.如果该函数未显式返回任何内容,则将隐式返回该对象.您可以查看详细说明.

When you call a function with the new operator, a new object is created and passed as the execution context (aka this) to the function. This object is then implicitly returned if the function does not return anything explicitly. You can have a look at the detailed explanation.

还要考虑到箭头函数永远不能用作构造函数.

摘自Jest文档中的模拟函数:

From Jest documentation for mock functions:

mockFn.mock.instances

一个数组,其中包含使用new从此模拟函数实例化的所有对象实例.

An array that contains all the object instances that have been instantiated from this mock function using new.

因此,Jest模拟函数将存储在 instances 属性中的对象实例列表(将被传递给该函数)(作为 this 传递的新创建的对象每次使用 new 运算符调用该函数时.

So, Jest mock functions are storing in the instances attribute the list of object instances that are being passed to the function (the newly created object that is passed as this to the function) every time you call it with the new operator.

但是您的构造函数没有使用 this 对象,因此它保持为空.这就是为什么当您检查 Dog.mock.instances [0] .name 时,您会得到 undefined 的原因.如果稍微更改构造函数以将 name 属性分配给 this 对象,则可以看到测试通过:

But your constructor is not using the this object, so it remains empty. That is why when you check Dog.mock.instances[0].name you are getting undefined. If you change slightly your constructor to assign the name attribute to the this object you can see that your test passes:

Dog = jest.fn(function() {
    this.name = "spike";
    return{
        name:"spike",
        bark:function(){
            return "bhow " +this.name;
        }
    }
})

很少使用从构造函数中显式返回对象的方法.定义构造函数的最常用方法是将其属性分配给 this 对象.因此,解决您的问题的方法是将您的构造函数更改为:

Returning explicitly an object from a constructor function as you are doing is rarely used. The most usual way to define a constructor is to assign its properties to the this object. So, a solution to your problem would be to change your constructor function to:

Dog = jest.fn(function() {
    this.name = "spike";
    this.bark = function(){
        return "bhow " +this.name;
    }
})

如果不想更改构造函数的定义,另一种解决方案是使用结果

Another solution, if you don't want to change the definition of your constructor function, would be to use the results attribute of the mock function in your test:

test("testing foo",()=>{
    const result = foo();
    expect(Dog).toHaveBeenCalledTimes(1);
    expect(result).toBe("bhow spike");

    expect(Dog.mock.instances.length).toBe(1);

    expect(Dog.mock.results[0].value.name).toBe("spike");
});

这篇关于尝试访问mockedFunction.mock.instaces属性在Jest中给出了未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆