SeekanDestroy
SeekanDestroy

Reputation: 611

Jest: mock high order function

I'm trying to mock a third party library to write a integration test using Jest

Prod. code section:

const processedContent = await remark().use(html).process(content);

So I want to mock remark as a HOF to return a function (use) to return another function (process)

My approach:

remark.mockImplementationOnce(() => {
      return function use() {
        return function process() {
          return testMessageContent;
        };
      };
    })

So if console.log(remark) I can see how it returns a function, but when I try to console.log(remark().use(), I'm getting:

TypeError: (0 , _remark.default)(...).use is not a function

  72 |     //const ans = await addReply(content, comment, userInfo._id);
  73 | 
> 74 |     console.log(remark().use());

And if I:

const use = remark()
const process = use()
const message = process()

Everything works fine. I'm not getting why this dont work. Any help will be trully appreciated!

Thanks in advance!

Upvotes: 1

Views: 643

Answers (1)

Estus Flask
Estus Flask

Reputation: 222780

The mock doesn't return an object with use method and that named function is called use doesn't change the way it works.

It should be:

remark.mockImplementationOnce(() => {
      return { use() {
        return { process() {
          return testMessageContent;
        } };
      } };
    })

Upvotes: 1

Related Questions