Skip to content

test(svelte-query/createQueries): add test for not fetching queries with different durations when 'isRestoring' is true#10387

Merged
sukvvon merged 1 commit intomainfrom
test/svelte-query-createQueries-different-durations-isRestoring
Apr 5, 2026
Merged

test(svelte-query/createQueries): add test for not fetching queries with different durations when 'isRestoring' is true#10387
sukvvon merged 1 commit intomainfrom
test/svelte-query-createQueries-different-durations-isRestoring

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented Apr 5, 2026

🎯 Changes

  • Add test to verify that createQueries does not fetch queries with different durations (sleep(10) and sleep(20)) during the restoring period when isRestoring is true
  • Matches the existing react-query test pattern for useQueries with IsRestoringProvider
  • Verifies at three time points (0ms, 11ms, 21ms) that queries remain unfetched

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Tests
    • Added test coverage to verify query behavior during restoration periods, ensuring correct pending and idle states are maintained.

…ith different durations when 'isRestoring' is true
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 5, 2026

📝 Walkthrough

Walkthrough

A Vitest test case was added to the createQueries test suite for svelte-query that validates query behavior during the isRestoring state. The test renders an example with two queries having staggered promise resolution times and verifies queries remain pending with idle fetch status while isRestoring is active.

Changes

Cohort / File(s) Summary
Test Coverage
packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts
Added Vitest case verifying that createQueries does not fetch while isRestoring is true, asserting pending status, idle fetchStatus, undefined data, and zero query function invocations across queries with staggered resolution (10ms and 20ms).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

Suggested labels

package: svelte-query

Poem

🐰 With timers fake and queries two,
We test the restoring state so true,
No fetches spring while I restore,
The pending truth we now explore!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the test addition for verifying that createQueries does not fetch queries with different durations during the isRestoring period.
Description check ✅ Passed The description follows the template structure with all required sections completed. It provides clear context for the changes, matches the existing test pattern, and includes proper checklist and release impact information.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/svelte-query-createQueries-different-durations-isRestoring

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Apr 5, 2026

View your CI Pipeline Execution ↗ for commit 45b46a7

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 53s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-05 06:57:01 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

🚀 Changeset Version Preview

No changeset entries found. Merging this PR will not cause a version bump for any packages.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 5, 2026

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10387

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10387

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@10387

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10387

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10387

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10387

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10387

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10387

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10387

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10387

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10387

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10387

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10387

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10387

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10387

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10387

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10387

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10387

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10387

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10387

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10387

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10387

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10387

commit: 45b46a7

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

size-limit report 📦

Path Size
react full 11.98 KB (0%)
react minimal 9.01 KB (0%)

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts`:
- Around line 974-1017: The test enables fake timers with vi.useFakeTimers() but
restores them unconditionally at the end, risking leakage if an assertion
throws; wrap the test body that uses vi.useFakeTimers(), the calls to
vi.advanceTimersByTimeAsync(...) and all assertions in a try block and call
vi.useRealTimers() in a finally block so timers are always restored; locate the
code around vi.useFakeTimers(), the advanceTimers calls and the final
vi.useRealTimers() to implement the try/finally.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6c73de61-7455-43ea-9d7c-38581703f51b

📥 Commits

Reviewing files that changed from the base of the PR and between 422a997 and 45b46a7.

📒 Files selected for processing (1)
  • packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts

Comment on lines +974 to +1017
vi.useFakeTimers()

const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))

const rendered = render(IsRestoringExample, {
props: { queryFn1, queryFn2 },
})

await vi.advanceTimersByTimeAsync(0)

expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)

await vi.advanceTimersByTimeAsync(11)

expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)

await vi.advanceTimersByTimeAsync(10)

expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)

vi.useRealTimers()
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Protect fake-timer cleanup with try/finally to avoid cross-test leakage.

If any assertion fails before Line 1016, fake timers stay enabled and may cascade failures into later tests. Wrap the body in try/finally and restore timers in finally.

Suggested fix
   it('should not fetch queries with different durations for the duration of the restoring period when isRestoring is true', async () => {
-    vi.useFakeTimers()
-
-    const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
-    const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
-
-    const rendered = render(IsRestoringExample, {
-      props: { queryFn1, queryFn2 },
-    })
-
-    await vi.advanceTimersByTimeAsync(0)
-
-    expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
-    expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
-    expect(queryFn1).toHaveBeenCalledTimes(0)
-    expect(queryFn2).toHaveBeenCalledTimes(0)
-
-    await vi.advanceTimersByTimeAsync(11)
-
-    expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
-    expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
-    expect(queryFn1).toHaveBeenCalledTimes(0)
-    expect(queryFn2).toHaveBeenCalledTimes(0)
-
-    await vi.advanceTimersByTimeAsync(10)
-
-    expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
-    expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
-    expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
-    expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
-    expect(queryFn1).toHaveBeenCalledTimes(0)
-    expect(queryFn2).toHaveBeenCalledTimes(0)
-
-    vi.useRealTimers()
+    vi.useFakeTimers()
+    try {
+      const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
+      const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
+
+      const rendered = render(IsRestoringExample, {
+        props: { queryFn1, queryFn2 },
+      })
+
+      await vi.advanceTimersByTimeAsync(0)
+
+      expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
+      expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
+      expect(queryFn1).toHaveBeenCalledTimes(0)
+      expect(queryFn2).toHaveBeenCalledTimes(0)
+
+      await vi.advanceTimersByTimeAsync(11)
+
+      expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
+      expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
+      expect(queryFn1).toHaveBeenCalledTimes(0)
+      expect(queryFn2).toHaveBeenCalledTimes(0)
+
+      await vi.advanceTimersByTimeAsync(10)
+
+      expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
+      expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
+      expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
+      expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
+      expect(queryFn1).toHaveBeenCalledTimes(0)
+      expect(queryFn2).toHaveBeenCalledTimes(0)
+    } finally {
+      vi.useRealTimers()
+    }
   })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
vi.useFakeTimers()
const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
const rendered = render(IsRestoringExample, {
props: { queryFn1, queryFn2 },
})
await vi.advanceTimersByTimeAsync(0)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
await vi.advanceTimersByTimeAsync(10)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
vi.useRealTimers()
})
vi.useFakeTimers()
try {
const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
const rendered = render(IsRestoringExample, {
props: { queryFn1, queryFn2 },
})
await vi.advanceTimersByTimeAsync(0)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
await vi.advanceTimersByTimeAsync(10)
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
expect(queryFn1).toHaveBeenCalledTimes(0)
expect(queryFn2).toHaveBeenCalledTimes(0)
} finally {
vi.useRealTimers()
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts`
around lines 974 - 1017, The test enables fake timers with vi.useFakeTimers()
but restores them unconditionally at the end, risking leakage if an assertion
throws; wrap the test body that uses vi.useFakeTimers(), the calls to
vi.advanceTimersByTimeAsync(...) and all assertions in a try block and call
vi.useRealTimers() in a finally block so timers are always restored; locate the
code around vi.useFakeTimers(), the advanceTimers calls and the final
vi.useRealTimers() to implement the try/finally.

@sukvvon sukvvon self-assigned this Apr 5, 2026
@sukvvon sukvvon merged commit 4b2cd73 into main Apr 5, 2026
10 checks passed
@sukvvon sukvvon deleted the test/svelte-query-createQueries-different-durations-isRestoring branch April 5, 2026 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant