-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for watch plugins #5895
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c483350
Add skeleton
rogeliog a247c97
Update based on feedback
rickhanlonii cdbd96c
Merge remote-tracking branch 'upstream/master' into plugins-docs
rickhanlonii aa43d42
fix lint
rickhanlonii 352677c
fix lint
rickhanlonii 9cee1de
Update WatchPlugins.md
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
--- | ||
id: watch-plugins | ||
title: Watch Plugins | ||
original_id: 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 | ||
|
||
```javascript | ||
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 | ||
|
||
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. | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
apply(jestHooks) {} | ||
} | ||
``` | ||
|
||
Below are the hooks available in Jest. | ||
|
||
#### `jestHooks.shouldRunTestSuite(testPath)` | ||
|
||
Returns a boolean (or `Promise<boolean>`) for handling asynchronous operations) | ||
to specify if a test should be run or not. | ||
|
||
For example: | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
apply(jestHooks) { | ||
jestHooks.shouldRunTestSuite(testPath => { | ||
return testPath.includes('my-keyword'); | ||
}); | ||
|
||
// or a promise | ||
jestHooks.shouldRunTestSuite(testPath => { | ||
return Promise.resolve(testPath.includes('my-keyword')); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
#### `jestHooks.testRunComplete(results)` | ||
|
||
Gets called at the end of every test run. It has the test results as an | ||
argument. | ||
|
||
For example: | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
apply(jestHooks) { | ||
jestHooks.testRunComplete(results => { | ||
this._hasSnapshotFailure = results.snapshot.failure; | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
#### `jestHooks.fileChange({projects})` | ||
|
||
Gets called whenever there is a change in the file system | ||
|
||
* `projects: Array<config: ProjectConfig, testPaths: Array<string>`: Includes | ||
all the test paths that Jest is watching. | ||
|
||
For example: | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
apply(jestHooks) { | ||
jestHooks.fileChange(({projects}) => { | ||
this._projects = projects; | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## Watch Menu Integration | ||
|
||
Custom watch plugins can also add or override functionality to the watch menu by | ||
specifying a key/prompt pair in `getUsageInfo` method and a `run` method for the | ||
execution of the key. | ||
|
||
### `getUsageInfo(globalConfig)` | ||
|
||
To add a key to the watch menu, implement the `getUsageInfo` method, returning a | ||
key and the prompt: | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
getUsageInfo(globalConfig) { | ||
return { | ||
key: 's'.codePointAt(0), | ||
prompt: 'do something', | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
This will add a line in the watch mode menu _(`› Press s to do something.`)_ | ||
|
||
```text | ||
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. | ||
``` | ||
|
||
**Note**: If the key for your plugin already exists as a default key, your | ||
plugin will override that key. | ||
|
||
### `run(globalConfig, updateConfigAndRun)` | ||
|
||
To handle key press events from the key returned by `getUsageInfo`, you can | ||
implement the `run` method. This method returns a `Promise<boolean>` that can be | ||
resolved when the plugin wants to return control to Jest. The `boolean` | ||
specifies if Jest should rerun the tests after it gets the control back. | ||
|
||
* `globalConfig`: A representation of Jest's current global configuration | ||
* `updateConfigAndRun`: Allows you to trigger a test run while the interactive | ||
plugin is running. | ||
|
||
```javascript | ||
class MyWatchPlugin { | ||
run(globalConfig, updateConfigAndRun) { | ||
// do something. | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is wrong, now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what should it be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just
key: 's'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beauty