Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: How to test function that returns a function

I have several functions I would like to test with Jest. All functions are functions that return functions.

A simple example:

export function csl(foo) {
  return function(bar) {
      return(bar)
     };
}

now I want to test if the input = the return is. I try it with:

  expect(() => csl("foo")).toBe("foo") // = received: [Function anonymous]

  expect(csl("foo")).toBe("foo") // = received: undefined

How I can test these functions?

like image 518
Ckappo Avatar asked Oct 21 '25 16:10

Ckappo


1 Answers

You need to call the returned function

expect(csl("foo")("bar")).toBe("bar")
                 ^^^^^^^
like image 162
Ji aSH Avatar answered Oct 23 '25 07:10

Ji aSH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!