跳至主要內容
版本:29.7

觀看外掛

Jest 觀看外掛系統提供一種方法,可連結到 Jest 的特定部分,並定義在按鍵時執行程式碼的觀看模式選單提示。結合這些功能,您可以開發符合工作流程的互動式體驗。

觀看外掛介面

class MyWatchPlugin {
// Add hooks to Jest lifecycle events
apply(jestHooks) {}

// Get the prompt information for interactive plugins
getUsageInfo(globalConfig) {}

// Executed when the key from `getUsageInfo` is input
run(globalConfig, updateConfigAndRun) {}
}

連結到 Jest

若要將你的手錶外掛程式連接到 Jest,請在 Jest 組態中的 watchPlugins 下方新增其路徑

jest.config.js
module.exports = {
// ...
watchPlugins: ['path/to/yourWatchPlugin'],
};

自訂手錶外掛程式可以將掛勾新增到 Jest 事件。這些掛勾可以在手錶模式選單中新增互動式按鍵時或不時新增。

apply(jestHooks)

可透過實作 apply 方法來附加 Jest 掛勾。此方法會接收 jestHooks 參數,讓外掛程式可以掛勾到測試執行生命週期的特定部分。

class MyWatchPlugin {
apply(jestHooks) {}
}

以下是 Jest 中可用的掛勾。

jestHooks.shouldRunTestSuite(testSuiteInfo)

傳回布林值 (或 Promise<boolean> 以處理非同步作業) 來指定測試是否應該執行。

例如

class MyWatchPlugin {
apply(jestHooks) {
jestHooks.shouldRunTestSuite(testSuiteInfo => {
return testSuiteInfo.testPath.includes('my-keyword');
});

// or a promise
jestHooks.shouldRunTestSuite(testSuiteInfo => {
return Promise.resolve(testSuiteInfo.testPath.includes('my-keyword'));
});
}
}

jestHooks.onTestRunComplete(results)

在每次測試執行結束時呼叫。它將測試結果作為參數。

例如

class MyWatchPlugin {
apply(jestHooks) {
jestHooks.onTestRunComplete(results => {
this._hasSnapshotFailure = results.snapshot.failure;
});
}
}

jestHooks.onFileChange({projects})

每當檔案系統有變更時呼叫

  • projects: Array<config: ProjectConfig, testPaths: Array<string>: 包含 Jest 正在監看的所有測試路徑。

例如

class MyWatchPlugin {
apply(jestHooks) {
jestHooks.onFileChange(({projects}) => {
this._projects = projects;
});
}
}

手錶選單整合

自訂手錶外掛程式也可以透過在 getUsageInfo 方法中指定按鍵/提示配對和用於執行按鍵的 run 方法,來新增或覆寫手錶選單的功能。

getUsageInfo(globalConfig)

若要將按鍵新增到手錶選單,請實作 getUsageInfo 方法,傳回按鍵和提示

class MyWatchPlugin {
getUsageInfo(globalConfig) {
return {
key: 's',
prompt: 'do something',
};
}
}

這會在手錶模式選單中新增一行(› 按下 s 執行某項動作。)

Watch Usage
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press s to do something. // <-- This is our plugin
› Press Enter to trigger a test run.
注意

如果外掛程式的按鍵已存在為預設按鍵,你的外掛程式將會覆寫該按鍵。

run(globalConfig, updateConfigAndRun)

若要處理由 getUsageInfo 傳回的按鍵所產生的按鍵按下事件,您可以實作 run 方法。此方法會傳回一個 Promise<boolean>,當外掛程式想要將控制權還給 Jest 時,可以解析此 Promise。boolean 會指定 Jest 在取回控制權後是否應重新執行測試。

  • globalConfig:Jest 目前全域設定的表示方式
  • updateConfigAndRun:讓您可以在互動式外掛程式執行期間觸發測試執行。
class MyWatchPlugin {
run(globalConfig, updateConfigAndRun) {
// do something.
}
}
注意

如果您呼叫 updateConfigAndRun,您的 run 方法不應解析為真值,因為這會觸發雙重執行。

已授權的設定金鑰

基於穩定性和安全性考量,只有部分全域設定金鑰可以使用 updateConfigAndRun 進行更新。目前的允許清單如下

自訂

可以透過您的 Jest 設定自訂外掛程式。

jest.config.js
module.exports = {
// ...
watchPlugins: [
[
'path/to/yourWatchPlugin',
{
key: 'k', // <- your custom key
prompt: 'show a custom prompt',
},
],
],
};

建議設定名稱

  • key:修改外掛程式金鑰。
  • prompt:允許使用者自訂外掛程式提示中的文字。

如果使用者提供自訂設定,系統會將其作為引數傳遞給外掛程式建構函式。

class MyWatchPlugin {
constructor({config}) {}
}

選擇一個好的金鑰

Jest 允許第三方外掛程式覆寫部分內建功能金鑰,但並非全部。特別是,下列金鑰不可覆寫 

  • c(清除篩選器樣式)
  • i(互動式更新不符合的快照)
  • q(退出)
  • u(更新所有不符合的快照)
  • w(顯示監控模式用法/可用動作)

下列內建功能金鑰可以覆寫 

  • p(測試檔名樣式)
  • t(測試名稱樣式)

任何未被內建功能使用的金鑰都可以宣告,正如您所預期的。請避免使用在各種鍵盤上難以取得的金鑰(例如 é),或預設情況下不可見的金鑰(例如,許多 Mac 鍵盤沒有 |\[ 等字元的視覺提示)

當發生衝突時

如果你的外掛程式嘗試覆寫保留的鍵,Jest 會以說明訊息傳出錯誤,類似於


Watch plugin YourFaultyPlugin attempted to register key `q`, that is reserved internally for quitting watch mode. Please change the configuration key for this plugin.

第三方外掛程式也被禁止覆寫已由在設定的外掛程式清單(watchPlugins 陣列設定)中較早出現的另一個第三方外掛程式保留的鍵。發生這種情況時,你還會收到一條錯誤訊息,試著幫助你修復它


Watch plugins YourFaultyPlugin and TheirFaultyPlugin both attempted to register key `x`. Please change the key configuration for one of the conflicting plugins to avoid overlap.