Repository: tanstack/query
File: packages/query-core/src/__tests__/queryClient.test.tsx (TypeScript)

Code before:

    test('should not update non existing queries', () => {
      const result = queryClient.setQueriesData<string>(
        { queryKey: ['key'] },
        'data',
      )

      expect(result).toEqual([])
      expect(queryClient.getQueryData(['key'])).toBe(undefined)
    })
  })

  describe('isFetching', () => {
    test('should return length of fetching queries', async () => {
      const key1 = queryKey()
      const key2 = queryKey()

      expect(queryClient.isFetching()).toBe(0)
      queryClient.prefetchQuery({
        queryKey: key1,
        queryFn: () => sleep(300).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(1)
      queryClient.prefetchQuery({
        queryKey: key2,
        queryFn: () => sleep(200).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(2)
      await waitFor(() => expect(queryClient.isFetching()).toEqual(1))
      await waitFor(() => expect(queryClient.isFetching()).toEqual(0))
    })
  })

  describe('getQueryData', () => {
    test('should return the query data if the query is found', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key, 'id'])).toBe('bar')
    })

    test('should return undefined if the query is not found', () => {
      const key = queryKey()
      expect(queryClient.getQueryData(key)).toBeUndefined()
    })

    test('should match exact by default', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key])).toBeUndefined()
    })
  })

Review comment (suggestion):
since we use real timers per default, please keep those times short (5ms and 10ms at most), or use fake timers and advance them how you want.

Code after:
      )

      expect(result).toEqual([])
      expect(queryClient.getQueryData(['key'])).toBe(undefined)
    })
  })

  describe('isFetching', () => {
    test('should return length of fetching queries', async () => {
      expect(queryClient.isFetching()).toBe(0)
      queryClient.prefetchQuery({
        queryKey: queryKey(),
        queryFn: () => sleep(10).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(1)
      queryClient.prefetchQuery({
        queryKey: queryKey(),
        queryFn: () => sleep(5).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(2)
      await vi.advanceTimersByTimeAsync(5)
      expect(queryClient.isFetching()).toEqual(1)
      await vi.advanceTimersByTimeAsync(5)
      expect(queryClient.isFetching()).toEqual(0)
    })
  })

  describe('getQueryData', () => {
    test('should return the query data if the query is found', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key, 'id'])).toBe('bar')
    })

    test('should return undefined if the query is not found', () => {
      const key = queryKey()
      expect(queryClient.getQueryData(key)).toBeUndefined()
    })

    test('should match exact by default', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key])).toBeUndefined()
    })
  })

  describe('ensureQueryData', () => {
    test('should return the cached query data if the query is found', async () => {
      const key = queryKey()
      const queryFn = () => Promise.resolve('data')