testing - How do I test Chrome Extension/Firefox WebExtension code? -
since firefox forcing me to, i'm rewriting extension use webextension apis, i.e. chrome's extension apis. want have automated testing. far i've tried this:
i've got package.json
npm
install depedencies:
{ "name": "extension-api-tests", "version": "0.0.1", "scripts": { "test": "karma start" }, "devdependencies": { "karma": "^1.3.0", "karma-firefox-launcher": "^1.0.0", "karma-mocha": "^1.3.0", "karma-sinon-chrome": "^0.2.0", "mocha": "^3.1.2", "sinon-chrome": "^2.1.2" } }
i've got karma.conf.js
set test runner:
module.exports = function(config) { config.set({ frameworks: ['mocha', 'sinon-chrome'], files: ['test.js'], reporters: ['dots'], autowatch: false, browsers: ['firefox'], singlerun: true, concurrency: infinity, }); };
and i've got basic tests:
describe('my frustration', () => { it('works when uses no apis', done => { done(); }); it('should respond messages!', done => { console.log('message test starting'); chrome.runtime.onmessage.addlistener((message, sender, sendresponse) => { console.log('received message'); sendresponse(true); }); chrome.runtime.sendmessage({}, result => { console.log('received response message'); done(); }); }); it('should open tab!', done => { console.log('tab test starting'); chrome.tabs.create({ 'active': true, 'url': 'http://www.example.com/', }, tab => { console.log('created tab:', tab); done(); }); }); });
this of course reduced test case. when run npm test
, (abbreviated slightly):
> extension-api-tests@0.0.1 test .../ext-test > karma start 25 07 2017 11:57:10.395:info [karma]: karma v1.7.0 server started @ http://0.0.0.0:9876/ 25 07 2017 11:57:10.397:info [launcher]: launching browser firefox unlimited concurrency 25 07 2017 11:57:10.404:info [launcher]: starting browser firefox 25 07 2017 11:57:14.687:info [firefox 54.0.0 (ubuntu 0.0.0)]: connected on socket iijnrrqfzwj68_gnaaaa id 42440302 . log: 'message test starting' firefox 54.0.0 (ubuntu 0.0.0) frustration should respond messages! failed timeout of 2000ms exceeded. async tests , hooks, ensure "done()" called; if returning promise, ensure resolves. log: 'tab test starting' firefox 54.0.0 (ubuntu 0.0.0) frustration should open tab! failed timeout of 2000ms exceeded. async tests , hooks, ensure "done()" called; if returning promise, ensure resolves. firefox 54.0.0 (ubuntu 0.0.0): executed 3 of 3 (2 failed) (3.998 secs / 4.001 secs) npm err! test failed. see above more details.
my tests try use extension apis fail. not (e.g.) chrome.runtime
not defined (but if remove 'sinon-chrome'
karma.conf.js), believe have sinon set up. apis never anything, never work. code want test all passing data around through these apis (especially messages, cross chrome/content boundary).
Comments
Post a Comment