thecodrr / fdir

⚡ The fastest directory crawler & globbing library for NodeJS. Crawls 1m files in < 1s
https://thecodrr.github.io/fdir/
MIT License
1.51k stars 58 forks source link

Ideas for mocking #78

Closed IanVS closed 2 years ago

IanVS commented 2 years ago

Hi, I'm attempting to convert from globby to fdir, and I'm curious if you have any ideas for a good way to mock fdir returns. Previously, we had:

(globby.sync as jest.Mock).mockReturnValueOnce([]);

But, since fdir uses a fluent api, I can't just mock fdir.sync. Is this something you've run into before? And if so, how did you handle it?

IanVS commented 2 years ago

I think I figured out something that works:

const syncMock = jest.fn();
const crawlerMock = jest.fn().mockImplementation(() => {
  return {
    sync: syncMock,
  };
});
jest.spyOn(fdir.prototype, 'crawl').mockImplementation(crawlerMock);

Happy to hear any other suggestions, but this seems to get me what I want.