Testing anonymous function equality with Jest Testing anonymous function equality with Jest javascript javascript

Testing anonymous function equality with Jest


In such situation, without rewriting your logic to use named functions, you don't really have another choice other than declaring the function before the test, e.g.

const foo = i => j => i * jconst 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 })  });    });

Alternatively, you can check whether obj.bar is any function, using expect.any(Function):

expect(obj).toEqual({ baz: expect.any(Function), boz: 1 })

which might actually make more sense depending on the context of the test.