Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f9ca64

Browse files
committed
Overhaul watch plugin hooks names
1 parent 15ed971 commit 4f9ca64

File tree

6 files changed

+33
-31
lines changed

6 files changed

+33
-31
lines changed

packages/jest-cli/src/__tests__/watch.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ describe('Watch mode flows', () => {
359359
});
360360

361361
it('allows WatchPlugins to hook into file system changes', async () => {
362-
const fileChange = jest.fn();
362+
const onFileChange = jest.fn();
363363
const pluginPath = `${__dirname}/__fixtures__/plugin_path_fs_change`;
364364
jest.doMock(
365365
pluginPath,
366366
() =>
367367
class WatchPlugin {
368368
apply(jestHooks) {
369-
jestHooks.fileChange(fileChange);
369+
jestHooks.onFileChange(onFileChange);
370370
}
371371
},
372372
{virtual: true},
@@ -383,7 +383,7 @@ describe('Watch mode flows', () => {
383383
stdin,
384384
);
385385

386-
expect(fileChange).toHaveBeenCalledWith({
386+
expect(onFileChange).toHaveBeenCalledWith({
387387
projects: [
388388
{
389389
config: contexts[0].config,
@@ -600,7 +600,7 @@ describe('Watch mode flows', () => {
600600
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
601601
runJestMock.mockReset();
602602

603-
hooks.getEmitter().testRunComplete({snapshot: {failure: true}});
603+
hooks.getEmitter().onTestRunComplete({snapshot: {failure: true}});
604604

605605
stdin.emit(KEYS.U);
606606
await nextTick();

packages/jest-cli/src/jest_hooks.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,64 @@ type JestHookExposedFS = {
1717
type FileChange = (fs: JestHookExposedFS) => void;
1818
type ShouldRunTestSuite = (testPath: string) => Promise<boolean>;
1919
type TestRunComplete = (results: AggregatedResult) => void;
20+
type AvailableHooks =
21+
| 'onFileChange'
22+
| 'onTestRunComplete'
23+
| 'shouldRunTestSuite';
2024

2125
export type JestHookSubscriber = {
22-
fileChange: (fn: FileChange) => void,
26+
onFileChange: (fn: FileChange) => void,
27+
onTestRunComplete: (fn: TestRunComplete) => void,
2328
shouldRunTestSuite: (fn: ShouldRunTestSuite) => void,
24-
testRunComplete: (fn: TestRunComplete) => void,
2529
};
2630

2731
export type JestHookEmitter = {
28-
fileChange: (fs: JestHookExposedFS) => void,
32+
onFileChange: (fs: JestHookExposedFS) => void,
33+
onTestRunComplete: (results: AggregatedResult) => void,
2934
shouldRunTestSuite: (testPath: string) => Promise<boolean>,
30-
testRunComplete: (results: AggregatedResult) => void,
3135
};
3236

3337
class JestHooks {
3438
_listeners: {
35-
fileChange: Array<FileChange>,
39+
onFileChange: Array<FileChange>,
40+
onTestRunComplete: Array<TestRunComplete>,
3641
shouldRunTestSuite: Array<ShouldRunTestSuite>,
37-
testRunComplete: Array<TestRunComplete>,
3842
};
3943

4044
constructor() {
4145
this._listeners = {
42-
fileChange: [],
46+
onFileChange: [],
47+
onTestRunComplete: [],
4348
shouldRunTestSuite: [],
44-
testRunComplete: [],
4549
};
4650
}
4751

48-
isUsed(hook: string) {
52+
isUsed(hook: AvailableHooks) {
4953
return this._listeners[hook] && this._listeners[hook].length;
5054
}
5155

5256
getSubscriber(): JestHookSubscriber {
5357
return {
54-
fileChange: fn => {
55-
this._listeners.fileChange.push(fn);
58+
onFileChange: fn => {
59+
this._listeners.onFileChange.push(fn);
60+
},
61+
onTestRunComplete: fn => {
62+
this._listeners.onTestRunComplete.push(fn);
5663
},
5764
shouldRunTestSuite: fn => {
5865
this._listeners.shouldRunTestSuite.push(fn);
5966
},
60-
testRunComplete: fn => {
61-
this._listeners.testRunComplete.push(fn);
62-
},
6367
};
6468
}
6569

6670
getEmitter(): JestHookEmitter {
6771
return {
68-
fileChange: fs =>
69-
this._listeners.fileChange.forEach(listener => listener(fs)),
72+
onFileChange: fs =>
73+
this._listeners.onFileChange.forEach(listener => listener(fs)),
74+
onTestRunComplete: results =>
75+
this._listeners.onTestRunComplete.forEach(listener =>
76+
listener(results),
77+
),
7078
shouldRunTestSuite: async testPath =>
7179
Promise.all(
7280
this._listeners.shouldRunTestSuite.map(listener =>
@@ -75,8 +83,6 @@ class JestHooks {
7583
).then(result =>
7684
result.every(shouldRunTestSuite => shouldRunTestSuite),
7785
),
78-
testRunComplete: results =>
79-
this._listeners.testRunComplete.forEach(listener => listener(results)),
8086
};
8187
}
8288
}

packages/jest-cli/src/plugins/update_snapshots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class UpdateSnapshotsPlugin extends BaseWatchPlugin {
3131
}
3232

3333
apply(hooks: JestHookSubscriber) {
34-
hooks.testRunComplete(results => {
34+
hooks.onTestRunComplete(results => {
3535
this._hasSnapshotFailure = results.snapshot.failure;
3636
});
3737
}

packages/jest-cli/src/plugins/update_snapshots_interactive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
5353
}
5454

5555
apply(hooks: JestHookSubscriber) {
56-
hooks.testRunComplete(results => {
56+
hooks.onTestRunComplete(results => {
5757
this._failedSnapshotTestAssertions = this.getFailedSnapshotTestAssertions(
5858
results,
5959
);

packages/jest-cli/src/types.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export type UsageData = {
1414
prompt: string,
1515
};
1616

17-
export type JestHooks = {
18-
testRunComplete: any,
19-
};
20-
2117
export interface WatchPlugin {
2218
+isInternal?: boolean;
2319
+apply?: (hooks: JestHookSubscriber) => void;

packages/jest-cli/src/watch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ export default function watch(
141141
let isWatchUsageDisplayed = false;
142142

143143
const emitFileChange = () => {
144-
if (hooks.isUsed('fileChange')) {
144+
if (hooks.isUsed('onFileChange')) {
145145
const projects = searchSources.map(({context, searchSource}) => ({
146146
config: context.config,
147147
testPaths: searchSource.findMatchingTests('').tests.map(t => t.path),
148148
}));
149-
hooks.getEmitter().fileChange({projects});
149+
hooks.getEmitter().onFileChange({projects});
150150
}
151151
};
152152

@@ -209,7 +209,7 @@ export default function watch(
209209
jestHooks: hooks.getEmitter(),
210210
onComplete: results => {
211211
isRunning = false;
212-
hooks.getEmitter().testRunComplete(results);
212+
hooks.getEmitter().onTestRunComplete(results);
213213

214214
// Create a new testWatcher instance so that re-runs won't be blocked.
215215
// The old instance that was passed to Jest will still be interrupted

0 commit comments

Comments
 (0)