-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
test(query-test-utils): init (internal package) #9095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
View your CI Pipeline Execution ↗ for commit eb1f343.
☁️ Nx Cloud last updated this comment at |
e3038ad to
b9e67d1
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9095 +/- ##
==========================================
+ Coverage 44.53% 44.55% +0.01%
==========================================
Files 204 207 +3
Lines 8150 8159 +9
Branches 1828 1820 -8
==========================================
+ Hits 3630 3635 +5
- Misses 4077 4081 +4
Partials 443 443 🚀 New features to boost your workflow:
|
73d0c27 to
7e9683f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is for an internal query-test-utils package that consolidates common test utilities such as sleep, mockVisibilityState, and others, which were previously duplicated across various TanStack Query packages. This helps reduce redundancy and improve test consistency across packages.
| /* istanbul ignore file */ | ||
|
|
||
| export function flushPromises(timeout = 0): Promise<unknown> { | ||
| return new Promise(function (resolve) { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } | ||
|
|
||
| export function simpleFetcher(): Promise<string> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| return resolve('Some data') | ||
| }, 0) | ||
| }) | ||
| } | ||
|
|
||
| export function getSimpleFetcherWithReturnData(returnData: unknown) { | ||
| return () => | ||
| new Promise((resolve) => setTimeout(() => resolve(returnData), 0)) | ||
| } | ||
|
|
||
| export function infiniteFetcher({ | ||
| pageParam, | ||
| }: { | ||
| pageParam?: number | ||
| }): Promise<string> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| return resolve('data on page ' + pageParam) | ||
| }, 0) | ||
| }) | ||
| } | ||
|
|
||
| export function rejectFetcher(): Promise<Error> { | ||
| return new Promise((_, reject) => { | ||
| setTimeout(() => { | ||
| return reject(new Error('Some error')) | ||
| }, 0) | ||
| }) | ||
| } | ||
|
|
||
| export function successMutator<T>(param: T): Promise<T> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| return resolve(param) | ||
| }, 0) | ||
| }) | ||
| } | ||
|
|
||
| export function errorMutator<T>(_: T): Promise<Error> { | ||
| return rejectFetcher() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| import { QueryClient } from '@tanstack/svelte-query' | ||
|
|
||
| import type { QueryClientConfig } from '@tanstack/svelte-query' | ||
|
|
||
| export function createQueryClient(config?: QueryClientConfig): QueryClient { | ||
| return new QueryClient(config) | ||
| } | ||
|
|
||
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| let queryKeyCount = 0 | ||
| export function queryKey() { | ||
| queryKeyCount++ | ||
| return [`query_${queryKeyCount}`] | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| export function createQueryClient(config?: QueryClientConfig): QueryClient { | ||
| return new QueryClient(config) | ||
| } | ||
|
|
||
| export function mockVisibilityState( | ||
| value: DocumentVisibilityState, | ||
| ): MockInstance<() => DocumentVisibilityState> { | ||
| return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| let queryKeyCount = 0 | ||
| export function queryKey(): Array<string> { | ||
| queryKeyCount++ | ||
| return [`query_${queryKeyCount}`] | ||
| } | ||
|
|
||
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| import { QueryClient } from '@tanstack/react-query' | ||
| import type { QueryClientConfig } from '@tanstack/react-query' | ||
|
|
||
| export function createQueryClient(config?: QueryClientConfig): QueryClient { | ||
| return new QueryClient(config) | ||
| } | ||
|
|
||
| let queryKeyCount = 0 | ||
| export function queryKey(): Array<string> { | ||
| queryKeyCount++ | ||
| return [`query_${queryKeyCount}`] | ||
| } | ||
|
|
||
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| export function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
| export function createQueryClient(config?: QueryClientConfig): QueryClient { | ||
| return new QueryClient(config) | ||
| } | ||
|
|
||
| function sleep(timeout: number): Promise<void> { | ||
| return new Promise((resolve, _reject) => { | ||
| setTimeout(resolve, timeout) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unify these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces an internal package (@tanstack/query-test-utils) for shared testing utilities, streamlining the usage of functions like sleep and standardizing the instantiation of QueryClient across various test files. Key changes include replacing duplicated local test utilities with imports from the new package, and updating test files to use new QueryClient() instead of a custom createQueryClient() helper.
Reviewed Changes
Copilot reviewed 116 out of 117 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/query-core/src/tests/infiniteQueryObserver.test.tsx | Replaced local test utils with shared ones and updated QueryClient instantiation. |
| packages/query-core/src/tests/infiniteQueryObserver.test-d.tsx | Updated imports and QueryClient instantiation for type tests. |
| packages/query-core/src/tests/infiniteQueryBehavior.test.tsx | Uniformly replaced local sleep and createQueryClient with shared utilities and new QueryClient. |
| packages/query-core/src/tests/hydration.test.tsx | Refactored dehydration/rehydration tests to use the shared sleep function and direct QueryClient instantiation. |
| packages/query-async-storage-persister/src/tests/utils.ts | Removed duplicated sleep function in favor of the shared package. |
| packages/query-async-storage-persister/src/tests/asyncThrottle.test.ts | Updated tests to use shared sleep function instead of local delay. |
| packages/angular-query-persist-client/src/tests/with-persist-query-client.test.ts | Adjusted import order to use shared query-test-utils. |
| packages/angular-query-persist-client/src/tests/utils.ts | Removed duplicate utility functions in favor of the shared package. |
| packages/angular-query-experimental/src/tests/* | Across multiple files, updated query and mutation functions to use the shared sleep utility and adjusted error/promise handling accordingly. |
| codecov.yml | Added the new package to the code coverage configuration. |
Files not reviewed (1)
- package.json: Language not supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you 🙏
89c0431 to
11f4dd5
Compare
…ntiation across test files
…ations in query tests
…ns in query tests
11f4dd5 to
4c28e94
Compare
* test(query-test-utils): init * refactor(tests): replace createQueryClient with new QueryClient instantiation across test files * refactor(tests): remove unused imports in utils test file * refactor(tests): replace fetcher functions with sleep-based implementations in query tests * refactor(tests): replace simpleFetcher with sleep-based implementations in query tests * fix(query-core): update test:build script to remove pnpm prefix * refactor(tests): extract query function into a variable for clarity in useQuery tests * chore(tsconfig): remove temporary Vitest TypeScript configuration file * chore: add @tanstack/query-test-utils as a devDependency in multiple packages --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Why
There are many shared test utilities like
sleep,mockVisibilityState, etc., duplicated across multiple@tanstack/*packages. This makes it hard to maintain consistency and often leads to minor discrepancies in behavior.To address this, I’ve made a new internal package:
@tanstack/query-test-utilsunderpackages/query-test-utils, which centralizes these test utilities.Ask
Please let me know if you're okay with adding this internal package for shared testing purposes. I'm happy to revise the approach if there's a better alternative!