🧪 [Testing] Add robust test suite for utils/handleError.js#58
🧪 [Testing] Add robust test suite for utils/handleError.js#58
Conversation
Co-authored-by: zknpr <96851588+zknpr@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis PR adds a new test file ( Key observations:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[handleError called] --> B[console.log header]
B --> C{isGraphError?\nstack includes 'graphql-request'\n&& response.errors exists}
C -- Yes --> D[console.error GraphQL errors\nwith chain or 'Unknown']
C -- No --> E[console.error error.toString]
D --> F{axiosError?\nresponse.data.message exists}
E --> F
F -- Yes --> G[console.log Axios message]
F -- No --> H[getStackMessage]
G --> H
H --> I{stack is null?}
I -- Yes --> J[return empty array]
I -- No --> K{checkExportKeys\nin stack?}
K -- Yes --> J
K -- No --> L[filter node_modules\nfirstNMStackMessage\nmay be undefined]
L --> M{message.length > 0?}
M -- Yes --> N[console.log Truncated stack\nnote: may include 'undefined']
M -- No --> O[skip stack log]
N --> P[process.exit 1]
O --> P
Last reviewed commit: 9c1025c |
| await t.test('Standard error handling', () => { | ||
| const error = new Error('Test standard error'); | ||
| error.stack = 'Error: Test standard error\n at Object.<anonymous> (/test/file.js:1:1)\n at Module._compile (node:internal/modules/cjs/loader:1:1)'; | ||
|
|
||
| handleError(error); | ||
|
|
||
| assert.strictEqual(exitCode, 1, 'process.exit should be called with 1'); | ||
| assert.ok(logMessages[0].includes('------ ERROR ------'), 'Should log error string'); | ||
| assert.strictEqual(errorMessages[0], 'Error: Test standard error', 'Should log error.toString()'); | ||
| }); |
There was a problem hiding this comment.
Test misses source-code bug: undefined in truncated stack output
The "Standard error handling" test does not assert anything about the truncated stack content, which lets a real bug in getStackMessage go undetected.
The bug: When no node-module frame is found in the stack, firstNMStackMessage is undefined. Line 36 of handleError.js then does message.push(firstNMStackMessage, ...stack), placing undefined at index 0. Calling stack.join('\n') on that array produces "undefined\n at Object.<anonymous> ..." — the literal string "undefined" is printed to the console.
Trace for this test's input stack (/test/file.js has no node_modules):
firstNMStackMessage→undefined(no node-module frames exist)message = [undefined, ' at Object.<anonymous>...'](length 2, so the truncated-stack branch is entered)console.log(stack.join('\n'))→'undefined\n at Object.<anonymous>...'
The test passes today only because no assertion covers the stack output. Adding a check would expose the issue:
| await t.test('Standard error handling', () => { | |
| const error = new Error('Test standard error'); | |
| error.stack = 'Error: Test standard error\n at Object.<anonymous> (/test/file.js:1:1)\n at Module._compile (node:internal/modules/cjs/loader:1:1)'; | |
| handleError(error); | |
| assert.strictEqual(exitCode, 1, 'process.exit should be called with 1'); | |
| assert.ok(logMessages[0].includes('------ ERROR ------'), 'Should log error string'); | |
| assert.strictEqual(errorMessages[0], 'Error: Test standard error', 'Should log error.toString()'); | |
| }); | |
| await t.test('Standard error handling', () => { | |
| const error = new Error('Test standard error'); | |
| error.stack = 'Error: Test standard error\n at Object.<anonymous> (/test/file.js:1:1)\n at Module._compile (node:internal/modules/cjs/loader:1:1)'; | |
| handleError(error); | |
| assert.strictEqual(exitCode, 1, 'process.exit should be called with 1'); | |
| assert.ok(logMessages[0].includes('------ ERROR ------'), 'Should log error string'); | |
| assert.strictEqual(errorMessages[0], 'Error: Test standard error', 'Should log error.toString()'); | |
| // Verify truncated stack does not contain the literal string 'undefined' | |
| const truncatedIdx = logMessages.findIndex(msg => msg.includes('Truncated error stack:')); | |
| if (truncatedIdx !== -1) { | |
| assert.ok(!logMessages[truncatedIdx + 1].startsWith('undefined'), 'Stack output should not start with "undefined"'); | |
| } | |
| }); |
| const stackLogIndex = logMessages.findIndex(msg => msg.includes('Truncated error stack:')) + 1; | ||
| const truncatedStack = logMessages[stackLogIndex]; | ||
|
|
||
| // First element should be the node_modules stack line | ||
| assert.ok(truncatedStack.includes('node_modules/axios')); | ||
| // internal/modules should be filtered out | ||
| assert.ok(!truncatedStack.includes('node:internal')); |
There was a problem hiding this comment.
Guard missing before calling .includes() on potentially undefined value
logMessages[stackLogIndex] is used without first verifying it exists. If the findIndex call returns -1 (message not found) or if there is no message immediately after the "Truncated error stack:" entry, truncatedStack will be undefined, and the subsequent .includes() calls will throw a TypeError — hiding the real test failure with a confusing crash message.
| const stackLogIndex = logMessages.findIndex(msg => msg.includes('Truncated error stack:')) + 1; | |
| const truncatedStack = logMessages[stackLogIndex]; | |
| // First element should be the node_modules stack line | |
| assert.ok(truncatedStack.includes('node_modules/axios')); | |
| // internal/modules should be filtered out | |
| assert.ok(!truncatedStack.includes('node:internal')); | |
| const stackIdx = logMessages.findIndex(msg => msg.includes('Truncated error stack:')); | |
| assert.ok(stackIdx !== -1, '"Truncated error stack:" label should be logged'); | |
| const truncatedStack = logMessages[stackIdx + 1]; | |
| assert.ok(truncatedStack !== undefined, 'Stack content log message should follow the label'); | |
| // First element should be the node_modules stack line | |
| assert.ok(truncatedStack.includes('node_modules/axios')); | |
| // internal/modules should be filtered out | |
| assert.ok(!truncatedStack.includes('node:internal')); |
🎯 What: Implemented missing test coverage for the
handleErrorutility inutils/handleError.js.📊 Coverage: Mocked
console.log,console.error, andprocess.exitcorrectly. Test cases cover happy path error strings, parsing and processing of GraphQL errors (with and without explicitly stated chains), Axios error handling, and correct mapping and truncation of error stacks.✨ Result: Enhanced test coverage ensures stability and predictable outcomes when dealing with formatted application panics in adapters.
PR created automatically by Jules for task 6414772691796114345 started by @zknpr