chore: upgrade nock to v14 and fix api-request unit tests#3153
chore: upgrade nock to v14 and fix api-request unit tests#3153lahirumaramba wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request upgrades the nock dependency to version 14 and updates the associated package-lock.json with new sub-dependencies. Corresponding unit tests in api-request.spec.ts were updated to handle HEAD requests without bodies and to simulate timeouts using replyWithError. Feedback was provided regarding the error normalization logic in mockRequestWithError, which currently lacks robust handling for string primitives or null/undefined inputs, potentially leading to runtime errors or incorrect property assignments.
| let errorInstance: Error; | ||
| if (err instanceof Error) { | ||
| errorInstance = err; | ||
| } else { | ||
| errorInstance = new Error(err.message || 'Test network error'); | ||
| Object.assign(errorInstance, err); | ||
| } |
There was a problem hiding this comment.
The normalization logic in mockRequestWithError has a few potential issues:
- If
erris a string,err.messageis undefined, causing the error message to default to'Test network error'. Additionally,Object.assignon a string primitive will copy its indexed characters as properties (e.g.,'0': 'f'), which is likely unintended. - If
errisnullorundefined, accessingerr.messagewill throw a runtime error.
Consider handling strings explicitly and adding a null check before property access.
let errorInstance: Error;
if (err instanceof Error) {
errorInstance = err;
} else if (typeof err === 'string') {
errorInstance = new Error(err);
} else {
errorInstance = new Error(err?.message || 'Test network error');
if (err && typeof err === 'object') {
Object.assign(errorInstance, err);
}
}
Upgrades
nocktov14.0.15and updatesHttpClientunit tests to align withnockv14's spec-compliant response handling and socket interception refactors.nockfrom^13.0.0to^14.0.15inpackage.json.mockRequestWithErrorto normalize mock error inputs into realErrorobjects, preventingnockv14 from silently dropping request error events.nockv13, a bug existed wherenockincorrectly returned a response body forHEADrequests ifrespDatawas supplied to.reply(), forcing the old test to assert thatresp.datawas parsed. Innockv14,nockcorrectly conforms to the HTTP specification and ignores any reply body forHEADrequests, resulting in an empty raw response ("").respData) from thenockreply. The test now correctly asserts thatresp.text === ''and that accessingresp.datathrows a missing data error.ETIMEDOUTerror usingreplyWithError(err)instead of relying on mock socket delay events, which were not reliably triggering undernockv14.