0

Firefoxは私を強制しているので、WebExtension API(ChromeのExtension API)を使用するように拡張機能を書き直しています。私は自動化されたテストをしたい。これまでのところ私はこれを試してみた:Chrome拡張機能/ Firefox WebExtensionコードをテストするにはどうすればよいですか?

npmがdepedenciesをインストールするように、私はpackage.jsonを持っている:

{ 
    "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" 
    } 
} 

私はそのテストランナーをセットアップするkarma.conf.js持っている:

module.exports = function(config) { 
    config.set({ 
    frameworks: ['mocha', 'sinon-chrome'], 
    files: ['test.js'], 
    reporters: ['dots'], 
    autoWatch: false, 
    browsers: ['Firefox'], 
    singleRun: true, 
    concurrency: Infinity, 
    }); 
}; 

そして、私が持っている基本的なテスト:

describe('my frustration',() => { 
    it('works when it uses no APIs', done => { 
    done(); 
    }); 

    it('should respond to 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 to message'); 
     done(); 
    }); 
    }); 

    it('should open a tab!', done => { 
    console.log('tab test starting'); 
    chrome.tabs.create({ 
     'active': true, 
     'url': 'http://www.example.com/', 
    }, tab => { 
     console.log('created a tab:', tab); 
     done(); 
    }); 
    }); 
}); 

これは、もちろん、テストケースが減少します。私はnpm testを実行すると、私は(少し省略)を取得:すべての失敗拡張APIを使用しようと

> [email protected] test .../ext-test 
> karma start 

25 07 2017 11:57:10.395:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/ 
25 07 2017 11:57:10.397:INFO [launcher]: Launching browser Firefox with 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 with id 42440302 
. 
LOG: 'message test starting' 
Firefox 54.0.0 (Ubuntu 0.0.0) my frustration should respond to messages! FAILED 
     Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
LOG: 'tab test starting' 
Firefox 54.0.0 (Ubuntu 0.0.0) my frustration should open a tab! FAILED 
     Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it 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 for more details. 

私のテストを。 ではないと言っています(たとえば、chrome.runtimeは定義されていませんが、karma.conf.jsから'sinon-chrome'を削除した場合)、私はsinonを設定していると思います。しかし、APIは決して何もしません。テストしたいコードはすべてで、これらのAPIを介してデータを(特にメッセージとして、クロム/コンテンツの境界を越えて)渡すことについてです。

+0

主な問題についてはコメントできませんが、解決したら、Chrome 49以降のすべてのバージョンのFirefoxでは、 'runtime.sendMessage'が独自のページ/フレームに送信されないことに注意してください。 – wOxxOm

+0

私は、sinonがテストを可能にするために実際のAPIと全く同じように動作しない偽物を提供すると推測しています。 – arantius

+0

確かに、sinonのドキュメントの例は、あなたが使用する簡単な呼び出しのようなものではありません。これは唯一のサポートされている方法だとは言えませんが、私はsinonは知らないのです。 – wOxxOm

答えて

0

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessageによると、sendMessageはPromiseを返します。あなたのログによると、

chrome.runtime.sendMessage({}, result => { 
    console.log('received response to message'); 
    done(); 
}); 

であなたの成功ハンドラは、テストを実行し、メインスレッドに比べて時間に呼び出されていません。あなたがいる限りのsendMessageが500ミリ秒以内に返すように

chrome.runtime.sendMessage({}, result => { 
    console.log('received response to message'); 
}); 

sleep(500).then(() => { 
    done(); 
}); 

のように、メインスレッドに

function sleep (time) { 
    return new Promise((resolve) => setTimeout(resolve, time)); 
} 

を睡眠を追加しようとする必要があり、これは動作するはずです。

関連する問題