Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrome.runtime.onMessage inconsistent callback parameters? #176

Open
dhakan opened this issue Mar 14, 2023 · 1 comment
Open

chrome.runtime.onMessage inconsistent callback parameters? #176

dhakan opened this issue Mar 14, 2023 · 1 comment
Labels

Comments

@dhakan
Copy link

dhakan commented Mar 14, 2023

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 :)

@RickyMarou
Copy link
Collaborator

Hey @dhakan - I just took over this project and moved it to https://github.com/RickyMarou/jest-webextension-mock

I couldn't move the issue tracker with it. Are you still interested in contributing? I think you might be onto something here, it looks like we should change the arguments of the onMessageListeners

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants