简体   繁体   English

如何对Firefox 57 WebExtensions进行单元测试?

[英]How to unittest Firefox 57 WebExtensions?

The older Firefox "Add-ons" API had a built-in unittest layer sdk/test that allowed testing. 较旧的Firefox“附加组件”API具有允许测试的内置单元sdk/testsdk/test This doesn't seem to be available any more. 这似乎不再可用。

Additionally the use of "package/require" allowed code to be separated into "js code-only" packages that were testable using node.js. 此外,使用“package / require”允许将代码分成“js code-only”包,这些包可以使用node.js进行测试。 The new, highly structured javascript doesn't share this. 新的,高度结构化的JavaScript不会分享这个。

My priorities are (highest to lowest): 我的优先事项是(从最高到最低):

  1. Algorithms, "business logic", eg parsing input data - no APIs needed - just JavaScript 算法,“业务逻辑”,例如解析输入数据 - 无需API - 只需JavaScript
  2. Internal logic - eg background scripts interacting with settings, etc. 内部逻辑 - 例如与设置交互的后台脚本等
  3. UI interactions - I can live without this, but would be nice to test 用户界面互动 - 没有这个我可以生活,但测试会很好

So how do people test their WebExtensions? 那么人们如何测试他们的WebExtensions?

Check out webextension-geckodriver for a worked example of functional testing. 查看webextension-geckodriver ,了解功能测试的实例。

If you want to test interaction with the webextension API you can either do it live (have a test page for your extension and get geckodriver to visit it, for example) or use a fake like sinon-webextension through webextension-jsdom . 如果您想测试与webextension API的交互,您可以直接进行(例如,为扩展设置测试页面并让geckodriver访问它),或者通过webextension-jsdom使用类似sinon- webextension的假设

To unit test algorithms, just import the functions using jest, mocha, or whatever node unit testing framework you prefer or add them to a test page that you can visit in the browser. 要单元测试算法,只需使用jest,mocha或您喜欢的任何节点单元测试框架导入函数,或将它们添加到您可以在浏览器中访问的测试页面。

A complete, but old, worked example of webext testing is here: example-webextension . 这里有一个完整但旧的有效的webext测试示例example-webextension

An example of tests in a real webextension using another fake: vim-vixen 使用另一个假的真实webextension中的测试示例: vim-vixen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM