reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.72k stars 1.17k forks source link

Test Failure: Mismatch in Error Message Formatting for Non-JSON Response in fetchBaseQuery #4631

Open Adagedo opened 1 month ago

Adagedo commented 1 month ago

When running tests for the FetchBaseQuery with a non-JSON body, there is a mismatch in the expected and received error messages. Specifically, the error formatting differs in how it handles unexpected tokens in the response.

Expected Error

"error": "SyntaxError: Unexpected token h in JSON at position 1"

Recieved Error

"error": "SyntaxError: Unexpected token 'h', \"this is not json!\" is not valid JSON"

Test Case: This occurs in the test fetchBaseQuery › non-JSON-body › success: should fail gracefully (default="json" responseHandler) at line 176 in src/query/tests/fetchBaseQuery.test.tsx.

       174 |       expect(res.meta?.request).toBeInstanceOf(Request)
➤ YN0000:       175 |       expect(res.meta?.response).toBeInstanceOf(Object)
➤ YN0000:     > 176 |       expect(res.error).toEqual({
➤ YN0000:           |                         ^
➤ YN0000:       177 |         status: 'PARSING_ERROR',
➤ YN0000:       178 |         error: 'SyntaxError: Unexpected token h in JSON at position 1',
➤ YN0000:       179 |         originalStatus: 200,
➤ YN0000: 

Impact: While this test failure does not affect runtime behavior, it might introduce inconsistencies between the expected and actual error handling, potentially impacting robustness in error reporting. This mismatch may cause confusion when debugging errors in production, as error messages are not formatted as expected.

EskiMojo14 commented 1 month ago

I'm pretty sure the error message is dependent on your environment since it's thrown directly by JSON.parse.

I'd be tempted to just expect.stringContaining("SyntaxError") it to be honest.

Adagedo commented 1 month ago

I have suspicions on my environment though I was not too certain.