Skip to content

Commit

Permalink
Overhaul watch plugin hooks names
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed May 24, 2018
1 parent 15ed971 commit 4f9ca64
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
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 4f9ca64

Please sign in to comment.