セットアップおよびティアダウンコードの出力
一部のコマンドは、使用される場合、追加のセットアップとティアダウンが必要になる場合があります。
たとえば、リダイレクトをカウントし、テストの最後にその数を表示するコマンドです。
ブラウザで実行する場合、再生イベントを使用できますが、ランナーを使用して実行する場合は、これを実現するために追加の手順が必要です。
セットアップとティアダウンのコード出力を実装する前に、出力の仕組みについて一般的な概念を理解しておくことをお勧めします(リンク)。
テストアーキテクチャ
内部的には、ランナーはjestを使用してテストを実行します。
一般的なテストは次のようになります。
// config emission
describe("suite name", () => {
beforeAll(async () => {
// suite before all emission
});
beforeEach(async () => {
// suite before each emission
});
afterEach(async () => {
// suite after each emission
});
afterAll(async () => {
// suite after all emission
});
it("test name", async () => {
// test setup
driver.doStuff();
....
expect();
// test teardown
});
});
上記の各コメントに、セットアップとティアダウンの一部としてコードを挿入できます。
設定の出力
設定出力リクエストを使用して、テストファイルの先頭にコードを出力します。これは、グローバル変数の設定や、さまざまなモジュールの`require`に使用されます。
{
action: "emit",
entity: "config",
project: {
name: "project name"
tests: []
}
}
action
-emit
、コードの出力が必要なアクションを示します。entity
-config
、出力するエンティティ。設定コードはテストファイルの先頭に配置されます。project
- エクスポートされたプロジェクトのデータ。
...レスポンス
sendResponse(`const myLibrary = require("my-library");`);
...スイートの出力
常にスイート出力を試してください。組み込みのjestメソッドである`beforeAll`、`beforeEach`、`afterAll`、`afterEach`を使用します。
{
action: "emit",
entity: "suite",
suite: {
name: "suite name"
tests: []
}
}
action
-emit
、コードの出力が必要なアクションを示します。entity
-suite
、出力するエンティティ。スイートコードは各テストの前後に、またはすべてのテストの前後に1回配置できます。suite
- エクスポートされたスイートのデータ。
...レスポンス
sendResponse({
beforeAll: "this will run once before all tests",
beforeEach: "this will run before every test",
afterEach: "this will run after every test",
afterAll: "this will run after all tests"
});
...テストの出力
このメソッドは、テストケースの`it`関数内にコードを出力するため、お勧めできません。
これは、可能な限りスイートレベルの`beforeEach`と`afterEach`を使用する場合、テストメトリクス(テストパフォーマンスなど)に悪影響を及ぼします。
{
action: "emit",
entity: "test",
test: {
id: "unique test identifier",
name: "test name",
commands: [
command: "commandId",
target: "specified target",
value: "specified value"
]
}
}
action
-emit
、コードの出力が必要なアクションを示します。entity
-suite
、出力するエンティティ。スイートコードは各テストの前後に、またはすべてのテストの前後に1回配置できます。test
- エクスポートされたテストのデータ、識別子、名前、およびコマンドのリスト。
...レスポンス
sendResponse({
setup: "this will run at the beginning of the test",
teardown: "this will run at the end of the test"
});