In my node.js app, I've written code to read files from a directory using await fs.readdir. When I try to test the code using jest, I can't mock the readdir function. I'm attaching the code.
const util = require('util');
const readdir = util.promisify(fs.readdir);
async function test(){
const files = await readdir("directory path")
return 'success'
}
How to mock readdir using jest
I've tried the below code, it doesn't seem to work
it('test read', async() => {
jest.spyOn(util, 'promisify').mockReturnValueOnce(fs.readdir);
await readdir.mockReturnValueOnce(files);
await expect(await Files.test()).toEqual("success");
})
I'm getting this error, ENOENT: no such file or directory, scandir ''. How to fix this? any ideas?