extend-chrome / jest-chrome

A complete mock of the Chrome API for Chrome extensions for use with Jest.
MIT License
124 stars 24 forks source link

Cannot read property 'addListener' of undefined chrome.storage.local.onChanged.addListener #24

Open juskek opened 1 year ago

juskek commented 1 year ago

Google search terms

jest-chrome chrome.storage.local.onChanged.addListener

Describe the bug

chrome.storage.local.onChanged is undefined

TypeError: Cannot read property 'addListener' of undefined

      166 |         };
      167 |
    > 168 |         chrome.storage.local.onChanged.addListener(

How do we reproduce?

import { renderHook, act } from "@testing-library/react";

import { usePopup } from "../usePopup";
import { chrome } from "jest-chrome";

describe("usePopupRecordBytes", () => {
    it("test", async () => {      
        chrome.storage.local.onChanged.addListener(
            (changes: {
                [key: string]: chrome.storage.StorageChange;
            }) => {
                console.log('foo')
            };
        );

        expect(chrome.storage.local.onChanged.addListener).toHaveBeenCalledTimes(1);
    });
});

Expected behavior

chrome.storage.local.onChanged to be defined so that listeners can be added

Actual behavior

Screenshots

image

Please complete the following information:

Additional context