發射設定和結束代碼
如果使用某些指令,可能需要額外的設定和結束。
例如,計算重新導向次數並在測試結束時列印次數的指令。
在瀏覽器中執行時,可以使用播放事件,但使用執行器執行時,需要額外的步驟才能達成此目的。
在實作發射設定和結束代碼之前,最好先了解發射運作的一般概念 (連結)。
測試架構
在幕後,執行器使用 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: []
}
}
動作-發射,表示需要發射程式碼的動作。實體-設定,要發射的實體,設定程式碼置於測試檔案的頂端。專案- 匯出的專案資料。
回應
sendResponse(`const myLibrary = require("my-library");`);
套件發射
嘗試總是使用套件發射,它使用內建的 jest 方法,beforeAll、beforeEach、afterAll 和 afterEach。
{
action: "emit",
entity: "suite",
suite: {
name: "suite name"
tests: []
}
}
動作-發射,表示需要發射程式碼的動作。entity-suite,要發射的實體,套件代碼可以在每個測試之前和之後執行,或在所有測試之前和之後執行一次。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"
]
}
}
動作-發射,表示需要發射程式碼的動作。entity-suite,要發射的實體,套件代碼可以在每個測試之前和之後執行,或在所有測試之前和之後執行一次。test- 匯出的測試資料、識別碼、名稱和命令清單。
回應
sendResponse({
setup: "this will run at the beginning of the test",
teardown: "this will run at the end of the test"
});
