有没有办法测试匿名函数的相等性jest@20
?
我正在尝试通过类似于以下的测试:
const foo = i => j => {return i*j}
const bar = () => {baz:foo(2), boz:1}
describe('Test anonymous function equality',()=>{
it('+++ foo', () => {
const obj = foo(2)
expect(obj).toBe(foo(2))
});
it('+++ bar', () => {
const obj = bar()
expect(obj).toEqual({baz:foo(2), boz:1})
});
});
目前产生:
● >>>Test anonymous function equality › +++ foo
expect(received).toBe(expected)
Expected value to be (using ===):
[Function anonymous]
Received:
[Function anonymous]
Difference:
Compared values have no visual difference.
● >>>Test anonymous function equality › +++ bar
expect(received).toBe(expected)
Expected value to be (using ===):
{baz: [Function anonymous], boz:1}
Received:
{baz: [Function anonymous], boz:1}
Difference:
Compared values have no visual difference.
在这种情况下,如果不重写逻辑以使用命名函数,除了在测试之前声明函数, e.g.
const foo = i => j => i * j
const foo2 = foo(2)
const bar = () => ({ baz: foo2, boz: 1 })
describe('Test anonymous function equality', () => {
it('+++ bar', () => {
const obj = bar()
expect(obj).toEqual({ baz: foo2, boz: 1 })
});
});
或者,您可以检查是否obj.bar
is any功能, using expect.any(Function)
:
expect(obj).toEqual({ baz: expect.any(Function), boz: 1 })
根据测试的上下文,这实际上可能更有意义。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)