solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.13k stars 143 forks source link

got `window.scrollTo` error when testing router useing vitest #276

Closed Nauxscript closed 1 year ago

Nauxscript commented 1 year ago

Describe the bug

I am attempting to test my navbar component, which is essentially implemented the same as the provided example. The test case is working well, but I have noticed an error in the logs:

Error: Not implemented: window.scrollTo
    at module.exports (/project/home/nauxscript/workspace/node_modules/.pnpm/jsdom@21.1.0/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
    at /project/home/nauxscript/workspace/node_modules/.pnpm/jsdom@21.1.0/node_modules/jsdom/lib/jsdom/browser/Window.js:897:7
    at scrollToHash (/project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/integration.js:25:16)
    at /project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/integration.js:78:9
    at /project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/integration.js:32:20
    at /project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/integration.js:8:69
    at navigateEnd (/project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/routing.js:327:17)
    at /project/home/nauxscript/workspace/node_modules/.pnpm/@solidjs+router@0.7.0_solid-js@1.6.10/node_modules/@solidjs/router/dist/routing.js:308:29 

and I found a same issue in #111

Your Example Website or App

https://codesandbox.io/p/sandbox/heuristic-pascal-ym4s44

Steps to Reproduce the Bug or Issue

  1. run the test by pnpm run test
  2. check out the logs

Expected behavior

expected no error in the logs

Screenshots or Videos

No response

Platform

Additional context

No response

ryansolid commented 1 year ago

This issue is a missing DOM API in the test environment. The router needs window.scrollTo but the testing setup isn't patching it. I am not sure how to do this with Vitest, but I found an old stack overflow: https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t This is a similar category of issue. But I am not sure what the steps are for Vitest.

This is an issue with the testing setup and not the router. @atk if you have any insights can you provide them. Otherwise I'm closing this.

atk commented 1 year ago

The error is thrown by jsdom. I might be able to circumvent it in our testing library, though, but I'm hesitant to include side effects by default. Maybe an extra import would work.