question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create a mock WebExtension API for developers to run unit tests with

See original GitHub issue

Use case: “As a WebExtension developer, I’d like to unit test my extension without launching a real web browser. I’d like to use a mock library to make assertions about my use of the WebExtension API”

(This issue would likely have nothing to do with the web-ext tool itself, I’m just not sure where else to track it.)

This library would let you interact with a mock WebExtension API and make assertions about the results. It would probably use something like sinon for the mocking part.

Schemas

The library should use schema files to fully sync up with all APIs as well as future APIs and API changes. This will make the mock API a very precise representation of the real API. Here are Firefox’s schema locations:

These schemas are identical to Chrome’s but in Firefox the namespacing is a bit different (browser, not chrome) and the asynchronous functions return promises, not callbacks. Note that some Chrome schemas are IDL files but most are JSON schema files.

Prior art

There are a few libraries but they are Chrome only and are either incomplete or don’t fully sync with schemas:

  • chrome-mock
    • uses schemas but seems incomplete
    • schema files are copy/pasted, not in sync with upstream source code
  • sinon-chrome
    • uses some schema files, may not be in sync with upstream source code

If it’s easier to patch an existing library, let’s do it!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:14
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
Lusitocommented, May 1, 2020

If anyone is interested, I started a new approach, feel free to take a look and give feedback.

Here’s the discussion on discourse: https://discourse.mozilla.org/t/introducing-mockzilla-and-mockzilla-webextension-for-easy-web-extension-testing-with-jest/59159

These are the projects: https://lusito.github.io/mockzilla-webextension/ https://lusito.github.io/mockzilla/

2reactions
stoicallycommented, Mar 3, 2018

@kumar303 I’ve created webextensions-jsdom and included an example webextension + test to illustrate how the several parts play together. Maybe that helps showing how that can be useful.

@balta2ar If you want to make an actual integration-test that you can start from nodejs with functional native messaging available, then that’s something I’m looking for as well. What I found so far regarding executing test in the actual WebExtension context in the browser is “Running tests from within the addon” in this official example. However, that only lets you execute tests encapsulated in the browser, you can’t wire it up with your client app or execute it from nodejs (and get actual browser context exposed) - at least I’m not aware of how that could be possible - although it looks like the already mentioned example-webextension has some code already doing exactly that.

What I imagine is something along the lines of

const webExt = require('web-ext/api');
webExt.loadTemporaryAddon('manifest.json');

Now webExt exposes e.g. .popup and .background, both also with the window and window.browser property. That would make it possible to easily execute feature/integration tests from nodejs within the actual browser environment. Though, maybe that’s already possible and I just missed it, because loading web-ext from NodeJS is already possible, it just don’t mentions if/how window/browser contexts from popup/background are exposed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Unit Test WebExtensions | Shing's Blog
I want to keep my tests as simple as possible, so I made some assumptions: I don't test WebExtension API calls. I keep...
Read more >
Unit Testing Browser Extensions - DEV Community ‍ ‍
A guide on how to setup unit testing for browser extensions. ... to access WebExtension APIs from a webpage when running in Chrome....
Read more >
Testing your web extension with integration tests - Medium
The best approach, I found so far, is to mock the tests with sinon and run them in a DOM emulation (Jest for...
Read more >
Introducing mockzilla and mockzilla-webextension for easy ...
With jest you can mock entire npm packages. But it's not very comfortable to use for a deeply nested API like browser ....
Read more >
How to unittest Firefox 57 WebExtensions? - Stack Overflow
If you want to test interaction with the webextension API you can either do it live (have a test page for your extension...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found