Skip to content

Instantly share code, notes, and snippets.

@apieceofbart
Created February 21, 2018 13:15
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apieceofbart/d28690d52c46848c39d904ce8968bb27 to your computer and use it in GitHub Desktop.
Save apieceofbart/d28690d52c46848c39d904ce8968bb27 to your computer and use it in GitHub Desktop.
mock lodash debounce in jest
// somewhere on top
import _ from 'lodash';
jest.unmock('lodash');
// then
_.debounce = jest.fn((fn) => fn);
@pietrofxq
Copy link

can you explain why this works? If I don't do that, I receive "Expected a function" error

@lfernandoavalos
Copy link

lfernandoavalos commented Oct 10, 2018

Because you're replacing the actual bottom line implementation, so in your tests when you call debounce it will try calling a function on an undefined variable.

import debouce
    from 'lodash/debounce';

// Tell jest to mock this import
jest.mock('lodash/debounce');

// Assign the import a new implementation, in this case it's execute the function given to you
debouce.mockImplementation(fn => fn);

@smanwaring
Copy link

This is great! What if I also want to mock the cancel method that is returned on the debounce instance?

@albertpak
Copy link

Maybe someone else finds it useful, but what worked for me is adding jest.mock('lodash.debounce', () => jest.fn(fn => fn)) to jest setup file

@barmettlerl
Copy link

You have to call the function and ignore the timer jest.mock('lodash/fp/debounce', () =>jest.fn((nr, fn) => fn())). This worked for me.

@marceloemanoel
Copy link

marceloemanoel commented Jun 21, 2019

This is great! What if I also want to mock the cancel method that is returned on the debounce instance?

@smanwaring I've managed to mock cancel like this:

import debounce from "lodash/debounce";

jest.mock("lodash/debounce", () =>
  jest.fn(fn => {
    fn.cancel = jest.fn();
    return fn;
  })
);

@mos-adebayo
Copy link

mos-adebayo commented Apr 22, 2020

This worked for me

jest.mock('lodash', () => ({
...require.requireActual('lodash'), debounce: fn => {
fn.cancel = jest.fn(); return fn;
},}));`

@iainure
Copy link

iainure commented May 29, 2020

This worked for me

jest.mock('lodash', () => ({ ...require.requireActual('lodash'), debounce: fn => { fn.cancel = jest.fn(); return fn; }, }));

Yep this is the one that finally sorted it for me too. Thanks a lot.

@afontcu
Copy link

afontcu commented Jun 6, 2020

Since require.requireActual was removed in Jest v26 (jestjs/jest#9854), I believe you should use jest.requireActual

@qweluke
Copy link

qweluke commented Jul 10, 2020

It all depends how you import debounce

for
import { debounce } from 'lodash'

add to the top of your test (after imports)

const lodash = require('lodash')
lodash.debounce = jest.fn(fn => fn)

for
import debounce from 'lodash/debounce';
use
jest.mock('lodash/debounce', () => jest.fn(fn => fn));

@onekiloparsec
Copy link

Thanks so much @qweluke. How hard it is to find this information.

@vv13
Copy link

vv13 commented Nov 26, 2020

jest.mock('lodash', () => {
  const module = jest.requireActual('lodash');
  module.debounce = jest.fn(fn => fn);
  return module;
});

@jabreezy
Copy link

You have to call the function and ignore the timer jest.mock('lodash/fp/debounce', () =>jest.fn((nr, fn) => fn())). This worked for me.

Thank you so much, this worked for me.

@jatin-maropost
Copy link

jest.mock('lodash.debounce', () => jest.fn(fn => fn))

This works , Thanks

@rishik30
Copy link

This worked for me wonderfully

jest.mock('lodash', () => ({
...require.requireActual('lodash'), debounce: fn => {
fn.cancel = jest.fn(); return fn;
},}));`

Thanks.

@Knogobert
Copy link

Updated version of @mos-adebayo's above that worked for me (using lodash-es)

jest.mock('lodash-es', () => ({
  ...jest.requireActual('lodash-es'),
  debounce: (fn) => {
    fn.cancel = jest.fn();
    return fn;
  },
}));

@pvev
Copy link

pvev commented Jun 4, 2022

You have to call the function and ignore the timer jest.mock('lodash/fp/debounce', () =>jest.fn((nr, fn) => fn())). This worked for me.

👍

@sumrender
Copy link

sumrender commented Apr 15, 2024

This worked for me perfectly:

import * as lodash from 'lodash-es';
// other imports...

jest.mock('lodash-es/debounce', () => ({
  default: jest.fn(fn => fn),
  __esModule: true
}));

// describe block...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment