SvelteStack / svelte-query

Performant and powerful remote data synchronization for Svelte
https://sveltequery.vercel.app
MIT License
813 stars 31 forks source link

How to test svelte-query using jest / svelte-testing-library #67

Closed mpiorowski closed 2 years ago

mpiorowski commented 2 years ago

Hello, I have setup up an application using this: https://github.com/mihar-22/svelte-jester#typescript So i got working jest with sveltekit, everything works fine when i render simple page.

But my problem is when i try to add svelte-query. I mocked http requestes using msw, but it seems like the useQuery is in never ending 'loading' state. No errors, no nothing, always loading.

Do i need to do something to make the query works? Maybe my render using svelte-testing-library is not picking up on subscription changes from the query?

I tried adding tick() function from svelte, but it didnt helped.

Would be really greatfull for some help.

mpiorowski commented 2 years ago

Sorry, everthing works perfectly :). If anyone will have a similar issue, I was missing:

trasherdk commented 2 years ago

import 'whatwg-fetch';

beforeAll(() => {
  Object.defineProperty(window, 'matchMedia', {
    writable: true,
    value: jest.fn().mockImplementation((query) => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: jest.fn(), // Deprecated
      removeListener: jest.fn(), // Deprecated
      addEventListener: jest.fn(),
      removeEventListener: jest.fn(),
      dispatchEvent: jest.fn(),
    })),
  });
});