-
Notifications
You must be signed in to change notification settings - Fork 2
test: fileQueries handling of a 500 Internal Server Error #286
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
Open
mkitti
wants to merge
5
commits into
main
Choose a base branch
from
mkitti-reproduce-500-error-frontend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a969071
test: add reproduction case for frontend 500 error handling (expected…
mkitti 3fcea15
test: refine reproduction test (rename and add assertion)
mkitti 807a9a5
style: apply prettier formatting
mkitti 3b37625
Merge branch 'main' into mkitti-reproduce-500-error-frontend
allison-truhlar de551aa
chore: relocate fileQueries test into frontend test dir
allison-truhlar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
frontend/src/__tests__/unitTests/fileQueries.errorHandling.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import useFileQuery from '../../queries/fileQueries'; | ||
| import * as utils from '@/utils'; | ||
|
|
||
| // Mock the utils module | ||
| vi.mock('@/utils', async () => { | ||
| const actual = await vi.importActual('@/utils'); | ||
| return { | ||
| ...actual, | ||
| sendFetchRequest: vi.fn(), | ||
| buildUrl: (path: string) => path, // Simple mock for buildUrl | ||
| makeMapKey: (type: string, name: string) => `${type}_${name}` | ||
| }; | ||
| }); | ||
|
|
||
| // Mock the context | ||
| const mockZonesAndFspData = { | ||
| 'fsp_test-fsp': { | ||
| name: 'test-fsp', | ||
| path: '/test/path', | ||
| type: 'posix' | ||
| } | ||
| }; | ||
|
|
||
| vi.mock('@/contexts/ZonesAndFspMapContext', () => ({ | ||
| useZoneAndFspMapContext: () => ({ | ||
| zonesAndFspQuery: { | ||
| data: mockZonesAndFspData | ||
| } | ||
| }) | ||
| })); | ||
|
|
||
| describe('useFileQuery 500 Error Reproduction', () => { | ||
| let queryClient: QueryClient; | ||
|
|
||
| beforeEach(() => { | ||
| queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: false // Disable retries for faster tests | ||
| } | ||
| } | ||
| }); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should handle 500 Internal Server Error correctly', async () => { | ||
| // Arrange | ||
| const fspName = 'test-fsp'; | ||
| const folderName = ''; | ||
|
|
||
| // Mock sendFetchRequest to return a 500 response with HTML body | ||
| // and a json() method that fails (as it tries to parse HTML) | ||
| const mockResponse = { | ||
| ok: false, | ||
| status: 500, | ||
| statusText: 'Internal Server Error', | ||
| json: async () => { | ||
| throw new SyntaxError('Unexpected token < in JSON at position 0'); | ||
| } | ||
| }; | ||
|
|
||
| vi.mocked(utils.sendFetchRequest).mockResolvedValue( | ||
| mockResponse as unknown as Response | ||
| ); | ||
|
|
||
| // Act | ||
| const { result } = renderHook(() => useFileQuery(fspName, folderName), { | ||
| wrapper: ({ children }) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| {children} | ||
| </QueryClientProvider> | ||
| ) | ||
| }); | ||
|
|
||
| // Assert | ||
| // We expect the query to eventually fail | ||
| await waitFor(() => expect(result.current.isError).toBe(true), { | ||
| timeout: 10000 | ||
| }); | ||
|
|
||
| // The bug is that it throws the SyntaxError from JSON parsing | ||
| // instead of the "Server returned 500" error. | ||
| // So this assertion confirms the BUG (on this branch) | ||
| // or confirms the FIX (if the message is clean). | ||
|
|
||
| // Since we want to REPRODUCE the failure, we check what it actually is. | ||
| // If the bug is present, the error message will be the SyntaxError. | ||
| // If the fix is present, it will be "Server returned 500 Internal Server Error". | ||
|
|
||
| const error = result.current.error; | ||
| console.log('Test caught error:', error?.message); | ||
|
|
||
| // We want to verify that the code *should* be handling this, | ||
| // so we Assert what we WANT (the fix). | ||
| // This test definition asserts the correct behavior. | ||
| // Running it on the buggy branch should FAIL this assertion. | ||
| expect(error?.message).toBe('Server returned 500 Internal Server Error'); | ||
| expect(error?.message).not.toContain('Unexpected token'); | ||
| }); | ||
| }, 15000); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think the likely next step here would be to remove or modify this line and keep the next line that checks for the abseence of
'Unexpected token'.The main point of the test is to make sure that we are not showing JSON parsing errors of other error messages. We may not want to test for a specific error message.