Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock fs.readdir in jest node.js

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?

like image 534
Anjana Avatar asked Dec 05 '25 14:12

Anjana


1 Answers

You can use the fs/promises module, so you don't need to promisify fs functions

// readdir.js
const fs = require('fs/promises');

async function load(directoryPath) {
  const files = await fs.readdir(directoryPath);
  return 'success';
}

module.exports = { load };

Notice the jest.mock instruction:

// readdir.test.js
const { load } = require('./readdir');
const fs = require('fs/promises');

jest.mock('fs/promises', () => ({
  readdir: jest.fn(),
}));

it('calls fs.readdir function with the correct argument', async () => {
  fs.readdir.mockResolvedValue();

  await load('my-path');

  expect(fs.readdir).toHaveBeenCalledWith('my-path');
});

it('returns correct result', async () => {
  fs.readdir.mockResolvedValue();

  const result = await load('x');

  expect(result).toEqual('success');
});

It runs correctly on my local machine, so if it does not run ok there, might be a configuration issue, not really a problem mocking the fs module

like image 163
tam.teixeira Avatar answered Dec 07 '25 07:12

tam.teixeira