bit*_*der 23 javascript unit-testing equality node.js jestjs
有没有办法测试匿名函数的相等性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})
});
});
Run Code Online (Sandbox Code Playgroud)
目前产量:
? >>>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.
Run Code Online (Sandbox Code Playgroud)
Wik*_*ski 26
在这种情况下,如果不重写逻辑以使用命名函数,除了在测试之前声明函数之外,您实际上没有其他选择,例如
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 })
});
});
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用以下方法检查是否obj.bar有任何功能expect.any(Function):
expect(obj).toEqual({ baz: expect.any(Function), boz: 1 })
Run Code Online (Sandbox Code Playgroud)
根据测试的上下文,这实际上可能更有意义.
| 归档时间: |
|
| 查看次数: |
13998 次 |
| 最近记录: |