diff --git a/apps/browser/src/autofill/deprecated/content/autofill-init.deprecated.spec.ts b/apps/browser/src/autofill/deprecated/content/autofill-init.deprecated.spec.ts index 6153a5c926f..96d5e85ca34 100644 --- a/apps/browser/src/autofill/deprecated/content/autofill-init.deprecated.spec.ts +++ b/apps/browser/src/autofill/deprecated/content/autofill-init.deprecated.spec.ts @@ -61,10 +61,13 @@ describe("AutofillInit", () => { autofillInit.init(); jest.advanceTimersByTime(250); - expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ - command: "bgCollectPageDetails", - sender: "autofillInit", - }); + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith( + { + command: "bgCollectPageDetails", + sender: "autofillInit", + }, + expect.any(Function), + ); }); it("registers a window load listener to collect the page details if the document is not in a `complete` ready state", () => { diff --git a/apps/browser/src/autofill/services/autofill-overlay-content.service.spec.ts b/apps/browser/src/autofill/services/autofill-overlay-content.service.spec.ts index 1d5ec605320..aedb9ae2d84 100644 --- a/apps/browser/src/autofill/services/autofill-overlay-content.service.spec.ts +++ b/apps/browser/src/autofill/services/autofill-overlay-content.service.spec.ts @@ -37,10 +37,9 @@ describe("AutofillOverlayContentService", () => { ); autofillInit = new AutofillInit(autofillOverlayContentService); autofillInit.init(); - sendExtensionMessageSpy = jest.spyOn( - autofillOverlayContentService as any, - "sendExtensionMessage", - ); + sendExtensionMessageSpy = jest + .spyOn(autofillOverlayContentService as any, "sendExtensionMessage") + .mockResolvedValue(undefined); Object.defineProperty(document, "readyState", { value: defaultWindowReadyState, writable: true, diff --git a/apps/browser/src/autofill/services/inline-menu-field-qualification.service.spec.ts b/apps/browser/src/autofill/services/inline-menu-field-qualification.service.spec.ts index feb78563db9..9b3ef4ce692 100644 --- a/apps/browser/src/autofill/services/inline-menu-field-qualification.service.spec.ts +++ b/apps/browser/src/autofill/services/inline-menu-field-qualification.service.spec.ts @@ -16,10 +16,8 @@ describe("InlineMenuFieldQualificationService", () => { forms: {}, fields: [], }); - chrome.runtime.sendMessage = jest.fn().mockImplementation((message) => ({ - result: message.command === "getInlineMenuFieldQualificationFeatureFlag", - })); inlineMenuFieldQualificationService = new InlineMenuFieldQualificationService(); + inlineMenuFieldQualificationService["inlineMenuFieldQualificationFlagSet"] = true; }); describe("isFieldForLoginForm", () => { diff --git a/apps/browser/src/autofill/utils/index.spec.ts b/apps/browser/src/autofill/utils/index.spec.ts index 47ab6183954..36d22ed0cd3 100644 --- a/apps/browser/src/autofill/utils/index.spec.ts +++ b/apps/browser/src/autofill/utils/index.spec.ts @@ -38,14 +38,24 @@ describe("generateRandomCustomElementName", () => { describe("sendExtensionMessage", () => { it("sends a message to the extension", async () => { - chrome.runtime.sendMessage = jest.fn().mockResolvedValue("sendMessageResponse"); + const extensionMessagePromise = sendExtensionMessage("some-extension-message"); - const response = await sendExtensionMessage("some-extension-message", { value: "value" }); + // Jest doesn't give anyway to select the typed overload of "sendMessage", + // a cast is needed to get the correct spy type. + const sendMessageSpy = jest.spyOn(chrome.runtime, "sendMessage") as unknown as jest.SpyInstance< + void, + [message: string, responseCallback: (response: string) => void], + unknown + >; - expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ - command: "some-extension-message", - value: "value", - }); + expect(sendMessageSpy).toHaveBeenCalled(); + + const [latestCall] = sendMessageSpy.mock.calls; + const responseCallback = latestCall[1]; + + responseCallback("sendMessageResponse"); + + const response = await extensionMessagePromise; expect(response).toEqual("sendMessageResponse"); }); });