From d84993c22b7087513b8a055a4be12c315c4512ad Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Sun, 13 Aug 2023 12:20:29 +0200 Subject: [PATCH] Define signals in tests cases --- packages/api/test/unit/client/fetch.test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/api/test/unit/client/fetch.test.ts b/packages/api/test/unit/client/fetch.test.ts index dac702da2291..e0f87e1c57e2 100644 --- a/packages/api/test/unit/client/fetch.test.ts +++ b/packages/api/test/unit/client/fetch.test.ts @@ -11,8 +11,7 @@ describe("FetchError", function () { id: string; url?: string; requestListener?: http.RequestListener; - abort?: true; - timeout?: true; + signalHandler?: () => AbortSignal; errorType: FetchErrorType; errorCode: string; expectCause: boolean; @@ -66,20 +65,20 @@ describe("FetchError", function () { }, { id: "Aborted request", - abort: true, requestListener: () => { // leave the request open until aborted }, + signalHandler: () => AbortSignal.abort(), errorType: "aborted", errorCode: "ERR_ABORTED", expectCause: false, }, { id: "Timeout request", - timeout: true, requestListener: () => { // leave the request open until timeout }, + signalHandler: () => AbortSignal.timeout(10), errorType: "timeout", errorCode: "ERR_TIMEOUT", expectCause: false, @@ -100,7 +99,7 @@ describe("FetchError", function () { }); for (const testCase of testCases) { - const {id, url = `http://localhost:${port}`, requestListener, abort, timeout} = testCase; + const {id, url = `http://localhost:${port}`, requestListener, signalHandler} = testCase; it(id, async function () { if (requestListener) { @@ -117,8 +116,7 @@ describe("FetchError", function () { ); } - const signal = abort ? AbortSignal.abort() : timeout ? AbortSignal.timeout(1) : null; - await expect(fetch(url, {signal})).to.be.rejected.then((error: FetchError) => { + await expect(fetch(url, {signal: signalHandler?.()})).to.be.rejected.then((error: FetchError) => { expect(error.type).to.be.equal(testCase.errorType); expect(error.code).to.be.equal(testCase.errorCode); if (testCase.expectCause) {