From 951cbd60c3258f5bee887039fa2f428ca504cfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 24 May 2018 15:52:44 +0100 Subject: [PATCH 1/3] Overhaul watch plugin hooks names --- packages/jest-cli/src/__tests__/watch.test.js | 8 ++-- packages/jest-cli/src/jest_hooks.js | 42 +++++++++++-------- .../jest-cli/src/plugins/update_snapshots.js | 2 +- .../plugins/update_snapshots_interactive.js | 2 +- packages/jest-cli/src/types.js | 4 -- packages/jest-cli/src/watch.js | 6 +-- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/jest-cli/src/__tests__/watch.test.js b/packages/jest-cli/src/__tests__/watch.test.js index f08c6240c1da..736eb59e17c8 100644 --- a/packages/jest-cli/src/__tests__/watch.test.js +++ b/packages/jest-cli/src/__tests__/watch.test.js @@ -359,14 +359,14 @@ describe('Watch mode flows', () => { }); it('allows WatchPlugins to hook into file system changes', async () => { - const fileChange = jest.fn(); + const onFileChange = jest.fn(); const pluginPath = `${__dirname}/__fixtures__/plugin_path_fs_change`; jest.doMock( pluginPath, () => class WatchPlugin { apply(jestHooks) { - jestHooks.fileChange(fileChange); + jestHooks.onFileChange(onFileChange); } }, {virtual: true}, @@ -383,7 +383,7 @@ describe('Watch mode flows', () => { stdin, ); - expect(fileChange).toHaveBeenCalledWith({ + expect(onFileChange).toHaveBeenCalledWith({ projects: [ { config: contexts[0].config, @@ -600,7 +600,7 @@ describe('Watch mode flows', () => { watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); runJestMock.mockReset(); - hooks.getEmitter().testRunComplete({snapshot: {failure: true}}); + hooks.getEmitter().onTestRunComplete({snapshot: {failure: true}}); stdin.emit(KEYS.U); await nextTick(); diff --git a/packages/jest-cli/src/jest_hooks.js b/packages/jest-cli/src/jest_hooks.js index c8ea97f327a7..95eb335463fc 100644 --- a/packages/jest-cli/src/jest_hooks.js +++ b/packages/jest-cli/src/jest_hooks.js @@ -17,56 +17,64 @@ type JestHookExposedFS = { type FileChange = (fs: JestHookExposedFS) => void; type ShouldRunTestSuite = (testPath: string) => Promise; type TestRunComplete = (results: AggregatedResult) => void; +type AvailableHooks = + | 'onFileChange' + | 'onTestRunComplete' + | 'shouldRunTestSuite'; export type JestHookSubscriber = { - fileChange: (fn: FileChange) => void, + onFileChange: (fn: FileChange) => void, + onTestRunComplete: (fn: TestRunComplete) => void, shouldRunTestSuite: (fn: ShouldRunTestSuite) => void, - testRunComplete: (fn: TestRunComplete) => void, }; export type JestHookEmitter = { - fileChange: (fs: JestHookExposedFS) => void, + onFileChange: (fs: JestHookExposedFS) => void, + onTestRunComplete: (results: AggregatedResult) => void, shouldRunTestSuite: (testPath: string) => Promise, - testRunComplete: (results: AggregatedResult) => void, }; class JestHooks { _listeners: { - fileChange: Array, + onFileChange: Array, + onTestRunComplete: Array, shouldRunTestSuite: Array, - testRunComplete: Array, }; constructor() { this._listeners = { - fileChange: [], + onFileChange: [], + onTestRunComplete: [], shouldRunTestSuite: [], - testRunComplete: [], }; } - isUsed(hook: string) { + isUsed(hook: AvailableHooks) { return this._listeners[hook] && this._listeners[hook].length; } getSubscriber(): JestHookSubscriber { return { - fileChange: fn => { - this._listeners.fileChange.push(fn); + onFileChange: fn => { + this._listeners.onFileChange.push(fn); + }, + onTestRunComplete: fn => { + this._listeners.onTestRunComplete.push(fn); }, shouldRunTestSuite: fn => { this._listeners.shouldRunTestSuite.push(fn); }, - testRunComplete: fn => { - this._listeners.testRunComplete.push(fn); - }, }; } getEmitter(): JestHookEmitter { return { - fileChange: fs => - this._listeners.fileChange.forEach(listener => listener(fs)), + onFileChange: fs => + this._listeners.onFileChange.forEach(listener => listener(fs)), + onTestRunComplete: results => + this._listeners.onTestRunComplete.forEach(listener => + listener(results), + ), shouldRunTestSuite: async testPath => Promise.all( this._listeners.shouldRunTestSuite.map(listener => @@ -75,8 +83,6 @@ class JestHooks { ).then(result => result.every(shouldRunTestSuite => shouldRunTestSuite), ), - testRunComplete: results => - this._listeners.testRunComplete.forEach(listener => listener(results)), }; } } diff --git a/packages/jest-cli/src/plugins/update_snapshots.js b/packages/jest-cli/src/plugins/update_snapshots.js index 00ff3b045a27..f405c1c54ede 100644 --- a/packages/jest-cli/src/plugins/update_snapshots.js +++ b/packages/jest-cli/src/plugins/update_snapshots.js @@ -31,7 +31,7 @@ class UpdateSnapshotsPlugin extends BaseWatchPlugin { } apply(hooks: JestHookSubscriber) { - hooks.testRunComplete(results => { + hooks.onTestRunComplete(results => { this._hasSnapshotFailure = results.snapshot.failure; }); } diff --git a/packages/jest-cli/src/plugins/update_snapshots_interactive.js b/packages/jest-cli/src/plugins/update_snapshots_interactive.js index 59f8d2312987..75c1c020cf15 100644 --- a/packages/jest-cli/src/plugins/update_snapshots_interactive.js +++ b/packages/jest-cli/src/plugins/update_snapshots_interactive.js @@ -53,7 +53,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin { } apply(hooks: JestHookSubscriber) { - hooks.testRunComplete(results => { + hooks.onTestRunComplete(results => { this._failedSnapshotTestAssertions = this.getFailedSnapshotTestAssertions( results, ); diff --git a/packages/jest-cli/src/types.js b/packages/jest-cli/src/types.js index d145c68135ff..51ec43e5da94 100644 --- a/packages/jest-cli/src/types.js +++ b/packages/jest-cli/src/types.js @@ -14,10 +14,6 @@ export type UsageData = { prompt: string, }; -export type JestHooks = { - testRunComplete: any, -}; - export interface WatchPlugin { +isInternal?: boolean; +apply?: (hooks: JestHookSubscriber) => void; diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index 9717940a6b8e..89f9ad058965 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -141,12 +141,12 @@ export default function watch( let isWatchUsageDisplayed = false; const emitFileChange = () => { - if (hooks.isUsed('fileChange')) { + if (hooks.isUsed('onFileChange')) { const projects = searchSources.map(({context, searchSource}) => ({ config: context.config, testPaths: searchSource.findMatchingTests('').tests.map(t => t.path), })); - hooks.getEmitter().fileChange({projects}); + hooks.getEmitter().onFileChange({projects}); } }; @@ -209,7 +209,7 @@ export default function watch( jestHooks: hooks.getEmitter(), onComplete: results => { isRunning = false; - hooks.getEmitter().testRunComplete(results); + hooks.getEmitter().onTestRunComplete(results); // Create a new testWatcher instance so that re-runs won't be blocked. // The old instance that was passed to Jest will still be interrupted From 7381f0ce217d81e0dd0669ea75118fdf7a613bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 24 May 2018 16:15:20 +0100 Subject: [PATCH 2/3] Update docs --- docs/WatchPlugins.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/WatchPlugins.md b/docs/WatchPlugins.md index 89eecb81802a..f93a47fbee91 100644 --- a/docs/WatchPlugins.md +++ b/docs/WatchPlugins.md @@ -65,7 +65,7 @@ class MyWatchPlugin { } ``` -#### `jestHooks.testRunComplete(results)` +#### `jestHooks.onTestRunComplete(results)` Gets called at the end of every test run. It has the test results as an argument. @@ -75,14 +75,14 @@ For example: ```javascript class MyWatchPlugin { apply(jestHooks) { - jestHooks.testRunComplete(results => { + jestHooks.onTestRunComplete(results => { this._hasSnapshotFailure = results.snapshot.failure; }); } } ``` -#### `jestHooks.fileChange({projects})` +#### `jestHooks.onFileChange({projects})` Gets called whenever there is a change in the file system @@ -94,7 +94,7 @@ For example: ```javascript class MyWatchPlugin { apply(jestHooks) { - jestHooks.fileChange(({projects}) => { + jestHooks.onFileChange(({projects}) => { this._projects = projects; }); } From 14c1239283424b1a4594c4eb961a4f696c7d06e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 24 May 2018 16:21:47 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7ae05772c6e..c24ded6d2ffa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,8 @@ ([#6221](https://github.com/facebook/jest/pull/6221)) * `[expect]` Improve return matchers ([#6172](https://github.com/facebook/jest/pull/6172)) +* `[jest-cli]` Overhaul watch plugin hooks names + ([#6249](https://github.com/facebook/jest/pull/6249)) ### Fixes