有没有办法在
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.
expect(''+obj).toEqual(''+foo(2))
,它比较的是函数字符串内容而不是函数本身。 - Malice.toEqual
内置方法来检查此类型的相等性。 - strider''+foo(1) === ''+foo(2)
的值为 true。 - strider