简体   繁体   中英

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?

You need to call the returned function

expect(csl("foo")("bar")).toBe("bar")
                 ^^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM