Skip to content

chrome.runtime.onMessage inconsistent callback parameters? #176

@dhakan

Description

@dhakan

Hi!

I'm working with the code located in the tabs.js module of this project as per:

sendMessage: jest.fn((tabId, message, cb) => {
    onMessageListeners.forEach((listener) => listener(tabId, message));
    if (cb !== undefined) {
      return cb();
    }
    return Promise.resolve();
  }),

From what I can see in the official docs for onMessage, the structure is as follows:

(message: any, sender: [MessageSender](https://developer.chrome.com/docs/extensions/reference/runtime/#type-MessageSender), sendResponse: function) => boolean | undefined

In my test if I run chrome.tabs.sendMessage(tabId, Message), the tabId is the first parameter of the listener, which is inconsistent with the way chrome.runtime.sendMessage calls its listener.

For more info, the listener I'm testing is

private async onMessage(
    message: Message,
    _: chrome.runtime.MessageSender,
    sendResponse: (response?: any) => void
  ) {
    console.log(message);
  }

There's also the issue with the sendResponse, which I don't believe is covered here?

Any guidance would be greatly appreciated :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions