From 4c85c4d08d62126ef3a5af535b57aae0a5d08825 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 28 Apr 2026 20:50:25 -0400 Subject: [PATCH] chore: resolve unbound-method lint warnings in tests (#12204) Capture jest mock references in local variables instead of asserting against unbound method references on mocked classes/instances. Co-authored-by: Claude Opus 4.7 --- .../processors/WebhookProcessor.test.ts | 30 ++++++------------- .../commands/documentPermanentDeleter.test.ts | 7 +++-- server/middlewares/timeout.test.ts | 21 +++++++------ server/models/Collection.test.ts | 4 +-- 4 files changed, 27 insertions(+), 35 deletions(-) diff --git a/plugins/webhooks/server/processors/WebhookProcessor.test.ts b/plugins/webhooks/server/processors/WebhookProcessor.test.ts index f8b6028375..4334c36cf3 100644 --- a/plugins/webhooks/server/processors/WebhookProcessor.test.ts +++ b/plugins/webhooks/server/processors/WebhookProcessor.test.ts @@ -5,9 +5,11 @@ import WebhookProcessor from "./WebhookProcessor"; jest.mock("../tasks/DeliverWebhookTask"); const ip = "127.0.0.1"; +const schedule = jest.fn(); beforeEach(() => { jest.resetAllMocks(); + DeliverWebhookTask.prototype.schedule = schedule; }); describe("WebhookProcessor", () => { @@ -29,12 +31,8 @@ describe("WebhookProcessor", () => { await processor.perform(event); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalled(); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalledWith({ + expect(schedule).toHaveBeenCalled(); + expect(schedule).toHaveBeenCalledWith({ event, subscriptionId: subscription.id, }); @@ -57,9 +55,7 @@ describe("WebhookProcessor", () => { await processor.perform(event); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalledTimes(0); + expect(schedule).toHaveBeenCalledTimes(0); }); it("it schedules a delivery for the event for each subscription", async () => { @@ -85,21 +81,13 @@ describe("WebhookProcessor", () => { await processor.perform(event); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalled(); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalledTimes(2); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalledWith({ + expect(schedule).toHaveBeenCalled(); + expect(schedule).toHaveBeenCalledTimes(2); + expect(schedule).toHaveBeenCalledWith({ event, subscriptionId: subscription.id, }); - expect( - jest.mocked(DeliverWebhookTask.prototype.schedule) - ).toHaveBeenCalledWith({ + expect(schedule).toHaveBeenCalledWith({ event, subscriptionId: subscriptionTwo.id, }); diff --git a/server/commands/documentPermanentDeleter.test.ts b/server/commands/documentPermanentDeleter.test.ts index 75a2a9f6bc..bf14cae22c 100644 --- a/server/commands/documentPermanentDeleter.test.ts +++ b/server/commands/documentPermanentDeleter.test.ts @@ -6,8 +6,11 @@ import documentPermanentDeleter from "./documentPermanentDeleter"; jest.mock("@server/queues/tasks/DeleteAttachmentTask"); +const schedule = jest.fn(); + beforeEach(() => { jest.resetAllMocks(); + DeleteAttachmentTask.prototype.schedule = schedule; }); describe("documentPermanentDeleter", () => { @@ -62,9 +65,7 @@ describe("documentPermanentDeleter", () => { await document.save(); const countDeletedDoc = await documentPermanentDeleter([document]); expect(countDeletedDoc).toEqual(1); - expect( - jest.mocked(DeleteAttachmentTask.prototype.schedule) - ).toHaveBeenCalledTimes(2); + expect(schedule).toHaveBeenCalledTimes(2); expect( await Document.unscoped().count({ where: { diff --git a/server/middlewares/timeout.test.ts b/server/middlewares/timeout.test.ts index 54d9827f14..30a95e74e2 100644 --- a/server/middlewares/timeout.test.ts +++ b/server/middlewares/timeout.test.ts @@ -6,9 +6,10 @@ describe("Timeout middleware", () => { const originalTimeout = 10000; const newTimeout = 1800000; // 30 minutes + const setTimeout = jest.fn(); const mockSocket = { timeout: originalTimeout, - setTimeout: jest.fn(), + setTimeout, } as unknown as Socket; const ctx = { @@ -27,20 +28,21 @@ describe("Timeout middleware", () => { ); // Should have set the new timeout - expect(mockSocket.setTimeout).toHaveBeenCalledWith(newTimeout); + expect(setTimeout).toHaveBeenCalledWith(newTimeout); // Should have called next expect(next).toHaveBeenCalled(); // Should have restored the original timeout - expect(mockSocket.setTimeout).toHaveBeenCalledWith(originalTimeout); + expect(setTimeout).toHaveBeenCalledWith(originalTimeout); }); it("should restore original timeout even if next throws", async () => { const originalTimeout = 10000; const newTimeout = 1800000; // 30 minutes + const setTimeout = jest.fn(); const mockSocket = { timeout: originalTimeout, - setTimeout: jest.fn(), + setTimeout, } as unknown as Socket; const ctx = { @@ -62,19 +64,20 @@ describe("Timeout middleware", () => { ).rejects.toThrow("Test error"); // Should have set the new timeout - expect(mockSocket.setTimeout).toHaveBeenCalledWith(newTimeout); + expect(setTimeout).toHaveBeenCalledWith(newTimeout); // Should have called next expect(next).toHaveBeenCalled(); // Should have restored the original timeout even after error - expect(mockSocket.setTimeout).toHaveBeenCalledWith(originalTimeout); + expect(setTimeout).toHaveBeenCalledWith(originalTimeout); }); it("should handle undefined original timeout", async () => { const newTimeout = 1800000; // 30 minutes + const setTimeout = jest.fn(); const mockSocket = { timeout: undefined, - setTimeout: jest.fn(), + setTimeout, } as unknown as Socket; const ctx = { @@ -93,10 +96,10 @@ describe("Timeout middleware", () => { ); // Should have set the new timeout - expect(mockSocket.setTimeout).toHaveBeenCalledWith(newTimeout); + expect(setTimeout).toHaveBeenCalledWith(newTimeout); // Should have called next expect(next).toHaveBeenCalled(); // Should have restored timeout to 0 when original was undefined - expect(mockSocket.setTimeout).toHaveBeenCalledWith(0); + expect(setTimeout).toHaveBeenCalledWith(0); }); }); diff --git a/server/models/Collection.test.ts b/server/models/Collection.test.ts index 9d8c99f216..07845f7772 100644 --- a/server/models/Collection.test.ts +++ b/server/models/Collection.test.ts @@ -310,9 +310,9 @@ describe("#removeDocument", () => { const document = await buildDocument({ collectionId: collection.id }); await collection.reload(); - jest.spyOn(collection, "save"); + const saveSpy = jest.spyOn(collection, "save"); await collection.deleteDocument(document); - expect(collection.save).toHaveBeenCalled(); + expect(saveSpy).toHaveBeenCalled(); }); it("should remove documents from root", async () => {