Skip to content

Commit c52d925

Browse files
rogeliogthymikee
authored andcommitted
Add documentation for watch plugins (#5895)
1 parent 77d2e8e commit c52d925

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

docs/WatchPlugins.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
id: watch-plugins
3+
title: Watch Plugins
4+
original_id: watch-plugins
5+
---
6+
7+
The Jest watch plugin system provides a way to hook into specific parts of Jest
8+
and to define watch mode menu prompts that execute code on key press. Combined,
9+
these features allow you to develop interactive experiences custom for your
10+
workflow.
11+
12+
## Watch Plugin Interface
13+
14+
```javascript
15+
class MyWatchPlugin {
16+
// Add hooks to Jest lifecycle events
17+
apply(jestHooks) {}
18+
19+
// Get the prompt information for interactive plugins
20+
getUsageInfo(globalConfig) {}
21+
22+
// Executed when the key from `getUsageInfo` is input
23+
run(globalConfig, updateConfigAndRun) {}
24+
}
25+
```
26+
27+
## Hooking into Jest
28+
29+
Custom watch plugins can add hooks to Jest events. These hooks can be added
30+
either with or without having an interactive key in the watch mode menu.
31+
32+
### `apply(jestHooks)`
33+
34+
Jest hooks can be attached by implementing the `apply` method. This method
35+
receives a `jestHooks` argument that allows the plugin to hook into specific
36+
parts of the lifecycle of a test run.
37+
38+
```javascript
39+
class MyWatchPlugin {
40+
apply(jestHooks) {}
41+
}
42+
```
43+
44+
Below are the hooks available in Jest.
45+
46+
#### `jestHooks.shouldRunTestSuite(testPath)`
47+
48+
Returns a boolean (or `Promise<boolean>`) for handling asynchronous operations)
49+
to specify if a test should be run or not.
50+
51+
For example:
52+
53+
```javascript
54+
class MyWatchPlugin {
55+
apply(jestHooks) {
56+
jestHooks.shouldRunTestSuite(testPath => {
57+
return testPath.includes('my-keyword');
58+
});
59+
60+
// or a promise
61+
jestHooks.shouldRunTestSuite(testPath => {
62+
return Promise.resolve(testPath.includes('my-keyword'));
63+
});
64+
}
65+
}
66+
```
67+
68+
#### `jestHooks.testRunComplete(results)`
69+
70+
Gets called at the end of every test run. It has the test results as an
71+
argument.
72+
73+
For example:
74+
75+
```javascript
76+
class MyWatchPlugin {
77+
apply(jestHooks) {
78+
jestHooks.testRunComplete(results => {
79+
this._hasSnapshotFailure = results.snapshot.failure;
80+
});
81+
}
82+
}
83+
```
84+
85+
#### `jestHooks.fileChange({projects})`
86+
87+
Gets called whenever there is a change in the file system
88+
89+
* `projects: Array<config: ProjectConfig, testPaths: Array<string>`: Includes
90+
all the test paths that Jest is watching.
91+
92+
For example:
93+
94+
```javascript
95+
class MyWatchPlugin {
96+
apply(jestHooks) {
97+
jestHooks.fileChange(({projects}) => {
98+
this._projects = projects;
99+
});
100+
}
101+
}
102+
```
103+
104+
## Watch Menu Integration
105+
106+
Custom watch plugins can also add or override functionality to the watch menu by
107+
specifying a key/prompt pair in `getUsageInfo` method and a `run` method for the
108+
execution of the key.
109+
110+
### `getUsageInfo(globalConfig)`
111+
112+
To add a key to the watch menu, implement the `getUsageInfo` method, returning a
113+
key and the prompt:
114+
115+
```javascript
116+
class MyWatchPlugin {
117+
getUsageInfo(globalConfig) {
118+
return {
119+
key: 's'.codePointAt(0),
120+
prompt: 'do something',
121+
};
122+
}
123+
}
124+
```
125+
126+
This will add a line in the watch mode menu _(`› Press s to do something.`)_
127+
128+
```text
129+
Watch Usage
130+
› Press p to filter by a filename regex pattern.
131+
› Press t to filter by a test name regex pattern.
132+
› Press q to quit watch mode.
133+
› Press s to do something. // <-- This is our plugin
134+
› Press Enter to trigger a test run.
135+
```
136+
137+
**Note**: If the key for your plugin already exists as a default key, your
138+
plugin will override that key.
139+
140+
### `run(globalConfig, updateConfigAndRun)`
141+
142+
To handle key press events from the key returned by `getUsageInfo`, you can
143+
implement the `run` method. This method returns a `Promise<boolean>` that can be
144+
resolved when the plugin wants to return control to Jest. The `boolean`
145+
specifies if Jest should rerun the tests after it gets the control back.
146+
147+
* `globalConfig`: A representation of Jest's current global configuration
148+
* `updateConfigAndRun`: Allows you to trigger a test run while the interactive
149+
plugin is running.
150+
151+
```javascript
152+
class MyWatchPlugin {
153+
run(globalConfig, updateConfigAndRun) {
154+
// do something.
155+
}
156+
}
157+
```

website/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"tutorial-react": "Testing React Apps",
3232
"tutorial-react-native": "Testing React Native Apps",
3333
"using-matchers": "Using Matchers",
34+
"watch-plugins": "Watch Plugins",
3435
"webpack": "Using with webpack",
3536
"Docs": "Docs",
3637
"API": "API",

0 commit comments

Comments
 (0)