How to Mock the chokidar npm Package with Jest (2025 Guide)

If you're writing tests for file-watching logic in a Node.js app, you’ve probably encountered chokidar—a popular and performant wrapper around fs.watch and fs.watchFile. But how do you mock chokidar in Jest to simulate file changes without touching the filesystem?

In this guide, we’ll walk through mocking chokidar with Jest, step by step, like the senior developers we pretend to be in job interviews.


🧠 Why Mock chokidar?

chokidar.watch() returns a file watcher that listens for events like add, change, and unlink. In unit tests, we don't want real file system watchers—we want controlled mocks to simulate those events.


🚀 The Goal

We’ll mock chokidar so that:

  1. We can intercept calls to chokidar.watch.
  2. We can manually trigger events like add, change, etc.
  3. Our code under test reacts as if those file changes actually occurred.

📦 Sample Code Using chokidar

Let’s say we have a file watcher service like this:

// file-watcher.js
const chokidar = require('chokidar');

function watchFiles(path, onChange) {
  const watcher = chokidar.watch(path, {
    persistent: true,
  });

  watcher.on('change', onChange);
}

module.exports = { watchFiles };

Now let’s test it.


🧪 How to Mock chokidar with Jest

1. Create a Mock of chokidar

// __mocks__/chokidar.js
const EventEmitter = require('events');

const mockWatch = jest.fn(() => {
  const emitter = new EventEmitter();
  emitter.on = jest.fn(emitter.on.bind(emitter));
  emitter.close = jest.fn();
  return emitter;
});

module.exports = {
  watch: mockWatch,
};

This mock returns an EventEmitter, just like the real chokidar.watch does. It also mocks .on() and .close() so you can spy on them.

2. Tell Jest to Use Your Manual Mock

// file-watcher.test.js
jest.mock('chokidar'); // Looks for __mocks__/chokidar.js

const { watchFiles } = require('./file-watcher');
const chokidar = require('chokidar');

describe('file-watcher', () => {
  it('should call onChange when a file changes', () => {
    const mockCallback = jest.fn();

    watchFiles('/some/path', mockCallback);

    // Get the mocked watcher instance
    const mockWatcher = chokidar.watch.mock.results[0].value;

    // Simulate a 'change' event
    mockWatcher.emit('change', 'file.txt');

    expect(mockCallback).toHaveBeenCalledWith('file.txt');
  });
});

🛠 Bonus: Reset Mocks Between Tests

If you're writing multiple tests, add this to your beforeEach:

beforeEach(() => {
  jest.clearAllMocks();
});

📌 Key Takeaways

  • chokidar returns an EventEmitter. That’s your mocking entry point.
  • Manually emit events like watcher.emit('change', 'filename').
  • Use jest.mock() and place your custom mock inside __mocks__/chokidar.js.

🔍 SEO Keywords

  • How to mock chokidar in Jest
  • Jest chokidar mock example
  • chokidar unit testing with Jest
  • Mock file watcher Node.js
  • Simulate file change in Jest test

🎯 Final Thoughts

Mocking chokidar is all about faking the EventEmitter it returns. With Jest and a little elbow grease, you can test your file-watching logic without relying on actual filesystem changes.

Now go write tests like the senior dev you were born to be. Or at least pretend to be in the next code review