New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@webext-core/fake-browser

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@webext-core/fake-browser

In-memory fake of the Browser extension API

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
increased by7.27%
Maintainers
1
Weekly downloads
 
Created
Source

@webext-core/fake-browser

Fake implementation of the webextension-polyfill Browser API for testing.

pnpm i -D @webext-core/fake-browser

Usage

To use the fake object in tests, configure your testing framework to use fakeBrowser as the mock for webextension-polyfill. Then use Browser in production code and fakeBrowser to setup tests.

Vitest

  1. Define a global mock for the webextension-polyfill package:

    <rootDir>/__mocks__/webextension-polyfill.ts
    export { fakeBrowser as default } from '@webext-core/fake-browser';
    
  2. In your test file, actually use the mock:

    some.test.ts
    import { fakeBrowser } from '@webext-core/fake-browser';
    import { localExtStorage } from '@webext-core/storage';
    import { test, vi } from 'vitest';
    
    // Use webextension-polyfill in production code
    function isXyzEnabled(): Promise<boolean> {
      return localExtStorage.getItem('xyz');
    }
    
    // Enable the global mock
    vi.mock('webextension-polyfill');
    
    // use fakeBrowser in your tests for setup/validation
    describe('isXyzEnabled', () => {
      it('should return true when enabled', async () => {
        const expected = true;
        await fakeBrowser.storage.local.set({ xyz: expected });
    
        const actual = await isXyzEnabled();
    
        expect(actual).toBe(expected);
      });
    
      ...
    });
    

Implemented APIs

Implemented APIs will execute listeners when calling methods that would trigger them. For example, calling Browser.tabs.create(...) would fire Browser.tabs.onCreate.

  • alarms
  • runtime (partial)
  • storage
  • tabs
  • windows

Generated Stubs

Unimplemented APIs will throw an error when accessed, letting you know they are not implemented and recommending you implement a mock yourself.

Open a PR to add an fake implementation, or just overwrite the functions with your own mocks.

  • activityLog
  • bookmarks
  • action
  • browserAction
  • browserSettings
  • browsingData
  • captivePortal
  • clipboard
  • commands
  • contentScripts
  • contextualIdentities
  • cookies
  • declarativeContent
  • devtools
  • dns
  • downloads
  • events
  • experiments
  • extension
  • extensionTypes
  • find
  • geckoProfiler
  • history
  • i18n
  • identity
  • idle
  • management
  • manifest
  • contextMenus
  • menus
  • networkStatus
  • normandyAddonStudy
  • notifications
  • omnibox
  • pageAction
  • permissions
  • pkcs11
  • privacy
  • proxy
  • runtime (parital)
  • scripting
  • search
  • sessions
  • sidebarAction
  • theme
  • topSites
  • types
  • urlbar
  • userScripts
  • webNavigation
  • webRequest

Keywords

FAQs

Package last updated on 04 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc