[英]Testing the function type returned from another function
我有一个返回另一个函数的函数:
const returnedFunction = () => {}
const returnFunction = () => {
const function = returnedFunction();
// Do stuff
return function;
}
我想测试从返回函数的类型returnFunction
的类型为returnedFunction
。 Jest 似乎在回应中提到了这一点:
expect(received).toBe(expected) // Object.is equality
Expected: "[Function returnedFunction]"
Received: [Function returnedFunction]
但我不知道如何匹配它们。
函数通过引用进行比较,因此如果您在returnedFunction
函数和测试中构造函数,即使它们看起来相同,也不会被视为相等。
您应该引入一些在测试和代码之间共享参考的方法。 例如,
// Note that sharedFn can now be used in your test for comparison
const sharedFn = () => {};
const returnedFunction = () => { return sharedFn; };
...
const received = returnFunction();
expec(received).toBe(sharedFn);
注意function
是javascript中的保留关键字,变量不能命名为function
我不确定你的意思是什么
是
returnedFunction
函数类型
你需要知道哪个函数被调用了吗? 除非您保留对您的函数的引用(例如在一个对象中),或者为它们分配唯一标识符,否则您不能真正将函数、事件与toString()
相等,这将仅保证两个函数的字符串表示(代码)是相同的。
我会尝试:
let returnedFunction = () => {};
returnedFunction.id = "returnedFunction";
const returnFunction = () => {
const function = returnedFunction;
// Do stuff
return function;
}
// getting id of the returned function
returnFunction().id
但我不清楚那个目标......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.