Skip to content

Commit

Permalink
Overhaul watch plugin hooks names (jestjs#6249)
Browse files Browse the repository at this point in the history
* Overhaul watch plugin hooks names

* Update docs

* update changelog
  • Loading branch information
thymikee authored and cpojer committed May 24, 2018
1 parent dc4df77 commit 3089fcb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
* `[jest-mock]` Include tracked call results in serialized mock
([#6244](https://github.com/facebook/jest/pull/6244))

Expand Down
8 changes: 4 additions & 4 deletions docs/WatchPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -94,7 +94,7 @@ For example:
```javascript
class MyWatchPlugin {
apply(jestHooks) {
jestHooks.fileChange(({projects}) => {
jestHooks.onFileChange(({projects}) => {
this._projects = projects;
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -383,7 +383,7 @@ describe('Watch mode flows', () => {
stdin,
);

expect(fileChange).toHaveBeenCalledWith({
expect(onFileChange).toHaveBeenCalledWith({
projects: [
{
config: contexts[0].config,
Expand Down Expand Up @@ -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();
Expand Down
42 changes: 24 additions & 18 deletions packages/jest-cli/src/jest_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,64 @@ type JestHookExposedFS = {
type FileChange = (fs: JestHookExposedFS) => void;
type ShouldRunTestSuite = (testPath: string) => Promise<boolean>;
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<boolean>,
testRunComplete: (results: AggregatedResult) => void,
};

class JestHooks {
_listeners: {
fileChange: Array<FileChange>,
onFileChange: Array<FileChange>,
onTestRunComplete: Array<TestRunComplete>,
shouldRunTestSuite: Array<ShouldRunTestSuite>,
testRunComplete: Array<TestRunComplete>,
};

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 =>
Expand All @@ -75,8 +83,6 @@ class JestHooks {
).then(result =>
result.every(shouldRunTestSuite => shouldRunTestSuite),
),
testRunComplete: results =>
this._listeners.testRunComplete.forEach(listener => listener(results)),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/plugins/update_snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UpdateSnapshotsPlugin extends BaseWatchPlugin {
}

apply(hooks: JestHookSubscriber) {
hooks.testRunComplete(results => {
hooks.onTestRunComplete(results => {
this._hasSnapshotFailure = results.snapshot.failure;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
}

apply(hooks: JestHookSubscriber) {
hooks.testRunComplete(results => {
hooks.onTestRunComplete(results => {
this._failedSnapshotTestAssertions = this.getFailedSnapshotTestAssertions(
results,
);
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-cli/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export type UsageData = {
prompt: string,
};

export type JestHooks = {
testRunComplete: any,
};

export interface WatchPlugin {
+isInternal?: boolean;
+apply?: (hooks: JestHookSubscriber) => void;
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3089fcb

Please sign in to comment.