From ac6a4c7d356a7d633ed6731afd26fb50d5453151 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Tue, 19 May 2026 15:42:13 -0400 Subject: [PATCH] chore: upgrade nock to v14 and fix api-request unit tests --- package-lock.json | 78 ++++++++++++++++++++++++++--- package.json | 2 +- test/unit/utils/api-request.spec.ts | 33 ++++++------ 3 files changed, 90 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2445788e91..ff3f95d376 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ "chalk": "^4.1.1", "child-process-promise": "^2.2.1", "del": "^6.0.0", - "eslint": "10.3.0", + "eslint": "^10.3.0", "firebase-token-generator": "^2.0.0", "globals": "^17.6.0", "gulp": "^5.0.0", @@ -63,7 +63,7 @@ "minimist": "^1.2.6", "mocha": "^11.0.0", "mz": "^2.7.0", - "nock": "^13.0.0", + "nock": "^14.0.15", "npm-run-all": "^4.1.5", "nyc": "^17.0.0", "request": "^2.75.0", @@ -1457,6 +1457,24 @@ "dev": true, "license": "MIT" }, + "node_modules/@mswjs/interceptors": { + "version": "0.41.9", + "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.41.9.tgz", + "integrity": "sha512-VVPPgHyQ6ShqnrmDWuxjmUIsO9gWyOZFmuOfLd9LfBGQJwZfy0gvv9pbHSJuoFNIYC7ZDX9aoFwowjcdSC4E8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@open-draft/deferred-promise": "^2.2.0", + "@open-draft/logger": "^0.3.0", + "@open-draft/until": "^2.0.0", + "is-node-process": "^1.2.0", + "outvariant": "^1.4.3", + "strict-event-emitter": "^0.5.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/@nodable/entities": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@nodable/entities/-/entities-2.1.0.tgz", @@ -1508,6 +1526,31 @@ "node": ">= 8" } }, + "node_modules/@open-draft/deferred-promise": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz", + "integrity": "sha512-CecwLWx3rhxVQF6V4bAgPS5t+So2sTbPgAzafKkVizyi7tlwpcFpdFqq+wqF2OwNBmqFuu6tOyouTuxgpMfzmA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@open-draft/logger": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@open-draft/logger/-/logger-0.3.0.tgz", + "integrity": "sha512-X2g45fzhxH238HKO4xbSr7+wBS8Fvw6ixhTDuvLd5mqh6bJJCFAPwU9mPDxbcrRtfxv4u5IHCEH77BmxvXmmxQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-node-process": "^1.2.0", + "outvariant": "^1.4.0" + } + }, + "node_modules/@open-draft/until": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@open-draft/until/-/until-2.1.0.tgz", + "integrity": "sha512-U69T3ItWHvLwGg5eJ0n3I62nWuE6ilHlmz7zM0npLBRvPRd7e6NYmg54vvRtP5mZG7kZqZCFVdsTWo7BPtBujg==", + "dev": true, + "license": "MIT" + }, "node_modules/@opentelemetry/api": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.1.tgz", @@ -7182,6 +7225,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-node-process": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/is-node-process/-/is-node-process-1.2.0.tgz", + "integrity": "sha512-Vg4o6/fqPxIjtxgUH5QLJhwZ7gW5diGCVlXpuUfELC62CuxM1iHcRe51f2W1FDy04Ai4KJkagKjx3XaqyfRKXw==", + "dev": true, + "license": "MIT" + }, "node_modules/is-number": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", @@ -8618,18 +8668,18 @@ "license": "MIT" }, "node_modules/nock": { - "version": "13.5.6", - "resolved": "https://registry.npmjs.org/nock/-/nock-13.5.6.tgz", - "integrity": "sha512-o2zOYiCpzRqSzPj0Zt/dQ/DqZeYoaQ7TUonc/xUPjCGl9WeHpNbxgVvOquXYAaJzI0M9BXV3HTzG0p8IUAbBTQ==", + "version": "14.0.15", + "resolved": "https://registry.npmjs.org/nock/-/nock-14.0.15.tgz", + "integrity": "sha512-S0a47C9pLvcYx/Ugf0H30BVBEcUgMMBDk9VJIDlJ8XGrfH2QDUD4Tgdp45qDIiHttokBG+IbsOtsvIjGR/j3bg==", "dev": true, "license": "MIT", "dependencies": { - "debug": "^4.1.0", + "@mswjs/interceptors": "^0.41.0", "json-stringify-safe": "^5.0.1", "propagate": "^2.0.0" }, "engines": { - "node": ">= 10.13" + "node": ">=18.20.0 <20 || >=20.12.1" } }, "node_modules/node-addon-api": { @@ -9401,6 +9451,13 @@ "safe-buffer": "~5.1.0" } }, + "node_modules/outvariant": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/outvariant/-/outvariant-1.4.3.tgz", + "integrity": "sha512-+Sl2UErvtsoajRDKCE5/dBz4DIvHXQQnAxtQTF04OJxY0+DyZXSo5P5Bb7XYWOh81syohlYL24hbDwxedPUJCA==", + "dev": true, + "license": "MIT" + }, "node_modules/own-keys": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/own-keys/-/own-keys-1.0.1.tgz", @@ -11367,6 +11424,13 @@ "text-decoder": "^1.1.0" } }, + "node_modules/strict-event-emitter": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/strict-event-emitter/-/strict-event-emitter-0.5.1.tgz", + "integrity": "sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ==", + "dev": true, + "license": "MIT" + }, "node_modules/string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", diff --git a/package.json b/package.json index 49aabe3e43..ead3e8fecb 100644 --- a/package.json +++ b/package.json @@ -272,7 +272,7 @@ "minimist": "^1.2.6", "mocha": "^11.0.0", "mz": "^2.7.0", - "nock": "^13.0.0", + "nock": "^14.0.15", "npm-run-all": "^4.1.5", "nyc": "^17.0.0", "request": "^2.75.0", diff --git a/test/unit/utils/api-request.spec.ts b/test/unit/utils/api-request.spec.ts index 3d1ad8e408..8cbc1f128d 100644 --- a/test/unit/utils/api-request.spec.ts +++ b/test/unit/utils/api-request.spec.ts @@ -92,9 +92,16 @@ function mockRequestWithHttpError( * @return {Object} A nock response object. */ function mockRequestWithError(err: any): nock.Scope { + let errorInstance: Error; + if (err instanceof Error) { + errorInstance = err; + } else { + errorInstance = new Error(err.message || 'Test network error'); + Object.assign(errorInstance, err); + } return nock('https://' + mockHost) .get(mockPath) - .replyWithError(err); + .replyWithError(errorInstance); } function mockHttp2SendRequestResponse( @@ -632,7 +639,6 @@ describe('HttpClient', () => { it('should make a HEAD request with the provided headers and data', () => { const reqData = { key1: 'value1', key2: 'value2' }; - const respData = { success: true }; const scope = nock('https://' + mockHost, { reqheaders: { 'Authorization': 'Bearer token', @@ -640,7 +646,7 @@ describe('HttpClient', () => { }, }).head(mockPath) .query(reqData) - .reply(200, respData, { + .reply(200, undefined, { 'content-type': 'application/json', }); mockedRequests.push(scope); @@ -656,8 +662,9 @@ describe('HttpClient', () => { }).then((resp) => { expect(resp.status).to.equal(200); expect(resp.headers['content-type']).to.equal('application/json'); - expect(resp.data).to.deep.equal(respData); - expect(resp.isJson()).to.be.true; + expect(resp.text).to.equal(''); + expect(() => { resp.data; }).to.throw('HTTP response missing data.'); + expect(resp.isJson()).to.be.false; }); }); @@ -741,14 +748,12 @@ describe('HttpClient', () => { }); it('should timeout when the response is repeatedly delayed', () => { - const respData = { foo: 'bar' }; + const timeoutErr = new Error('timeout of 50ms exceeded'); + (timeoutErr as any).code = 'ETIMEDOUT'; const scope = nock('https://' + mockHost) .get(mockPath) .times(5) - .delay(1000) - .reply(200, respData, { - 'content-type': 'application/json', - }); + .replyWithError(timeoutErr); mockedRequests.push(scope); const err = 'Error while making request: timeout of 50ms exceeded.'; @@ -762,14 +767,12 @@ describe('HttpClient', () => { }); it('should timeout when multiple socket timeouts encountered', () => { - const respData = { foo: 'bar timeout' }; + const timeoutErr = new Error('timeout of 50ms exceeded'); + (timeoutErr as any).code = 'ETIMEDOUT'; const scope = nock('https://' + mockHost) .get(mockPath) .times(5) - .delayConnection(2000) - .reply(200, respData, { - 'content-type': 'application/json', - }); + .replyWithError(timeoutErr); mockedRequests.push(scope); const err = 'Error while making request: timeout of 50ms exceeded.';