-
-
Save apieceofbart/d28690d52c46848c39d904ce8968bb27 to your computer and use it in GitHub Desktop.
// somewhere on top | |
import _ from 'lodash'; | |
jest.unmock('lodash'); | |
// then | |
_.debounce = jest.fn((fn) => fn); |
This worked for me
jest.mock('lodash', () =>
({
...require.requireActual('lodash'),
debounce: fn => {
fn.cancel = jest.fn();
return fn;
},
}));`
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.
Since require.requireActual
was removed in Jest v26 (jestjs/jest#9854), I believe you should use jest.requireActual
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));
Thanks so much @qweluke. How hard it is to find this information.
jest.mock('lodash', () => {
const module = jest.requireActual('lodash');
module.debounce = jest.fn(fn => fn);
return module;
});
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.
jest.mock('lodash.debounce', () => jest.fn(fn => fn))
This works , Thanks
This worked for me wonderfully
jest.mock('lodash', () =>
({
...require.requireActual('lodash'),
debounce: fn => {
fn.cancel = jest.fn();
return fn;
},
}));`
Thanks.
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;
},
}));
You have to call the function and ignore the timer
jest.mock('lodash/fp/debounce', () =>jest.fn((nr, fn) => fn()))
. This worked for me.
👍
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...
@smanwaring I've managed to mock cancel like this: