Create a mock WebExtension API for developers to run unit tests with
See original GitHub issueUse 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:
- Internal toolkit APIs: http://searchfox.org/mozilla-central/source/toolkit/components/extensions/schemas
- Browser UI APIs: http://searchfox.org/mozilla-central/source/browser/components/extensions/schemas
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:
- Created 7 years ago
- Reactions:14
- Comments:21 (11 by maintainers)
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/
@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
Now
webExt
exposes e.g..popup
and.background
, both also with thewindow
andwindow.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 loadingweb-ext
from NodeJS is already possible, it just don’t mentions if/how window/browser contexts from popup/background are exposed.