My React component calls window.scrollTo and when I run my jest tests, they pass, but there is a console error:
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: window.scrollTo
at Object.<anonymous>.module.exports (/private/var/www/samplesite.com/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at Window.scrollTo (/private/var/www/samplesite.com/node_modules/jsdom/lib/jsdom/browser/Window.js:544:7)
at scrollTo (/private/var/www/samplesite.com/back-end/utils/library.jsx:180:12)
at commitHookEffectList (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:17283:26)
at commitPassiveHookEffects (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:17307:3)
at Object.invokeGuardedCallbackImpl (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:74:10)
at invokeGuardedCallback (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:256:31)
at commitPassiveEffects (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:18774:9)
at wrapped (/private/var/www/samplesite.com/node_modules/react-dom/node_modules/scheduler/cjs/scheduler-tracing.development.js:207:34)
at flushPassiveEffects (/private/var/www/samplesite.com/node_modules/react-dom/cjs/react-dom.development.js:18822:5) undefined
I have tried mocking it in a few ways (both in the test itself and also in a jest setupFiles that gets called before each test) , but the console error will not go away:
Attempt 1:
let window = {};
window.scrollTo = jest.fn();
Attempt 2:
let scrollTo = jest.fn();
Object.defineProperty(window, "scrollTo", scrollTo);
Attempt 3:
delete global.window.scrollTo;
global.window.scrollTo = () => {};
Attempt 4:
global.window = {};
global.window.scrollTo = () => {};
How can i get rid of this console error? Thanks.