Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Not implemented: window.scrollTo. How do we remove this error from Jest test?

The error:

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29       Error: Not implemented: window.scrollTo           at module.exports (/Users/me/Projects/my-project/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)           at /Users/me/Projects/my-project/node_modules/jsdom/lib/jsdom/browser/Window.js:594:7 

Because we are using window.scrollTo(0,0).

Also getting Not implemented Navigation for:

window.location.replace(externa_link) same error happens with .assign.

I tried googling for solutions with react-router, but all examples use some component which contains window.location.

Is there a way to avoid this error? Or hide it?

I did some research and found that the Facebook team isn't going to address it. Is there a way to suppress these errors/warnings when running jest test?

Our code doesn't break and all tests pass otherwise.

like image 744
Leon Gaban Avatar asked Aug 01 '19 15:08

Leon Gaban


2 Answers

Try running the jest command with --env=jsdom. This will mock most browser functions and will solve your issues.

There are more ways of setting the test environment, take a look at:

https://jestjs.io/docs/en/configuration#testenvironment-string

Update

This worked for the window.scrollTo errors

https://qiita.com/akameco/items/0edfdae02507204b24c8

like image 71
Chris Avatar answered Sep 17 '22 19:09

Chris


At the top of the test file after the imports mock it like this :

window.scrollTo = jest.fn(); 

Then inside the describe add this:

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

so if you are also reseting all mocks after each you will end up with this ::

afterEach(() => {     jest.resetAllMocks();   });   afterAll(() => {     jest.clearAllMocks();   }); 
like image 35
Rolando Niubó Avatar answered Sep 17 '22 19:09

Rolando Niubó