Watch Plugins
The Jest watch plugin system provides a way to hook into specific parts of Jest and to define watch mode menu prompts that execute code on key press. Combined, these features allow you to develop interactive experiences custom for your workflow.
Watch Plugin Interface
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) {}
}
Hooking into Jest
To connect your watch plugin to Jest, add its path under watchPlugins
in your Jest configuration:
module.exports = {
// ...
watchPlugins: ['path/to/yourWatchPlugin'],
};
Custom watch plugins can add hooks to Jest events. These hooks can be added either with or without having an interactive key in the watch mode menu.
apply(jestHooks)
Jest hooks can be attached by implementing the apply
method. This method receives a jestHooks
argument that allows the plugin to hook into specific parts of the lifecycle of a test run.
class MyWatchPlugin {
apply(jestHooks) {}
}
Below are the hooks available in Jest.
jestHooks.shouldRunTestSuite(testSuiteInfo)
Returns a boolean (or Promise<boolean>
for handling asynchronous operations) to specify if a test should be run or not.
For example:
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'));
});
}
}