Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions docs/framework/angular/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ Retries slow failing tests because the default backoff runs three times. Set `re

## HttpClient & network stubs

Angular's `HttpClientTestingModule` plays nicely with PendingTasks. Register it alongside the Query provider and flush responses through `HttpTestingController`:
Angular's `HttpClient` testing utilities play nicely with PendingTasks. Register `provideHttpClient()` and `provideHttpClientTesting()` alongside the Query provider and flush responses through `HttpTestingController`:

```ts
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [provideTanStackQuery(queryClient)],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
provideTanStackQuery(queryClient),
],
})

const httpCtrl = TestBed.inject(HttpTestingController)
Expand All @@ -112,7 +115,8 @@ Use the same pattern for infinite queries: call `fetchNextPage()`, advance timer
const infinite = TestBed.runInInjectionContext(() =>
injectInfiniteQuery(() => ({
queryKey: ['pages'],
queryFn: ({ pageParam = 1 }) => fetchPage(pageParam),
queryFn: ({ pageParam }) => fetchPage(pageParam),
initialPageParam: 1,
getNextPageParam: (last, all) => all.length + 1,
})),
)
Expand Down Expand Up @@ -152,6 +156,6 @@ expect(mutation.data()).toBe('TEST')
- Fresh `QueryClient` per test (and clear it afterwards)
- Disable or control retries to avoid timeouts
- Advance timers + microtasks before `whenStable()` when using fake timers
- Use `HttpClientTestingModule` or your preferred mock to assert network calls
- Use `provideHttpClientTesting()` or your preferred mock to assert network calls
- Await `whenStable()` after every `refetch`, `fetchNextPage`, or mutation
- Prefer `TestBed.runInInjectionContext` for service tests and `fixture.whenStable()` for component tests
Loading