Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small polish to the plugins API #5572

Merged
merged 6 commits into from
Feb 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
A lot of renaming
rogeliog committed Feb 15, 2018
commit beb1e1ba88a0167243a4669ba3cf462ab1b37a25
24 changes: 12 additions & 12 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
@@ -244,14 +244,14 @@ describe('Watch mode flows', () => {
});

it('allows WatchPlugins to hook into JestHooks', async () => {
const registerHooks = jest.fn();
const apply = jest.fn();
const pluginPath = `${__dirname}/__fixtures__/plugin_path_register`;
jest.doMock(
pluginPath,
() =>
class WatchPlugin {
constructor() {
this.registerHooks = registerHooks;
this.apply = apply;
}
},
{virtual: true},
@@ -270,18 +270,18 @@ describe('Watch mode flows', () => {

await nextTick();

expect(registerHooks).toHaveBeenCalled();
expect(apply).toHaveBeenCalled();
});

it('triggers enter on a WatchPlugin when its key is pressed', async () => {
const showPrompt = jest.fn(() => Promise.resolve());
const runInteractive = jest.fn(() => Promise.resolve());
const pluginPath = `${__dirname}/__fixtures__/plugin_path`;
jest.doMock(
pluginPath,
() =>
class WatchPlugin1 {
constructor() {
this.showPrompt = showPrompt;
this.runInteractive = runInteractive;
}
getUsageData() {
return {
@@ -308,12 +308,12 @@ describe('Watch mode flows', () => {

await nextTick();

expect(showPrompt).toHaveBeenCalled();
expect(runInteractive).toHaveBeenCalled();
});

it('prevents Jest from handling keys when active and returns control when end is called', async () => {
let resolveShowPrompt;
const showPrompt = jest.fn(
const runInteractive = jest.fn(
() => new Promise(res => (resolveShowPrompt = res)),
);
const pluginPath = `${__dirname}/__fixtures__/plugin_path_1`;
@@ -322,9 +322,9 @@ describe('Watch mode flows', () => {
() =>
class WatchPlugin1 {
constructor() {
this.showPrompt = showPrompt;
this.runInteractive = runInteractive;
}
onData() {}
onKey() {}
getUsageData() {
return {
key: 's'.codePointAt(0),
@@ -342,9 +342,9 @@ describe('Watch mode flows', () => {
() =>
class WatchPlugin1 {
constructor() {
this.showPrompt = showPrompt2;
this.runInteractive = showPrompt2;
}
onData() {}
onKey() {}
getUsageData() {
return {
key: 'z'.codePointAt(0),
@@ -368,7 +368,7 @@ describe('Watch mode flows', () => {

stdin.emit(Number('s'.charCodeAt(0)).toString(16));
await nextTick();
expect(showPrompt).toHaveBeenCalled();
expect(runInteractive).toHaveBeenCalled();
stdin.emit(Number('z'.charCodeAt(0)).toString(16));
await nextTick();
expect(showPrompt2).not.toHaveBeenCalled();
6 changes: 3 additions & 3 deletions packages/jest-cli/src/base_watch_plugin.js
Original file line number Diff line number Diff line change
@@ -24,15 +24,15 @@ class BaseWatchPlugin {
this._stdout = stdout;
}

registerHooks(hooks: JestHookSubscriber) {}
apply(hooks: JestHookSubscriber) {}

getUsageData(globalConfig: GlobalConfig): ?UsageData {
return null;
}

onData(value: string) {}
onKey(value: string) {}

showPrompt(
runInteractive(
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
): Promise<void | boolean> {
2 changes: 1 addition & 1 deletion packages/jest-cli/src/plugins/quit.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
import BaseWatchPlugin from '../base_watch_plugin';

class QuitPlugin extends BaseWatchPlugin {
async showPrompt() {
async runInteractive() {
if (typeof this._stdin.setRawMode === 'function') {
this._stdin.setRawMode(false);
}
4 changes: 2 additions & 2 deletions packages/jest-cli/src/plugins/test_name_pattern.js
Original file line number Diff line number Diff line change
@@ -30,11 +30,11 @@ class TestNamePatternPlugin extends BaseWatchPlugin {
};
}

onData(key: string) {
onKey(key: string) {
this._prompt.put(key);
}

showPrompt(
runInteractive(
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
): Promise<void> {
4 changes: 2 additions & 2 deletions packages/jest-cli/src/plugins/test_path_pattern.js
Original file line number Diff line number Diff line change
@@ -31,11 +31,11 @@ class TestPathPatternPlugin extends BaseWatchPlugin {
};
}

onData(key: string) {
onKey(key: string) {
this._prompt.put(key);
}

showPrompt(
runInteractive(
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
): Promise<void> {
4 changes: 2 additions & 2 deletions packages/jest-cli/src/plugins/update_snapshots.js
Original file line number Diff line number Diff line change
@@ -12,15 +12,15 @@ import type {JestHookSubscriber} from '../jest_hooks';

class UpdateSnapshotsPlugin extends BaseWatchPlugin {
_hasSnapshotFailure: boolean;
showPrompt(
runInteractive(
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
): Promise<boolean> {
updateConfigAndRun({updateSnapshot: 'all'});
return Promise.resolve(false);
}

registerHooks(hooks: JestHookSubscriber) {
apply(hooks: JestHookSubscriber) {
this._hasSnapshotFailure = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary/intentional or a leftover from debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

hooks.testRunComplete(results => {
this._hasSnapshotFailure = results.snapshot.failure;
6 changes: 3 additions & 3 deletions packages/jest-cli/src/plugins/update_snapshots_interactive.js
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
this._snapshotInteractiveMode = new SnapshotInteractiveMode(this._stdout);
}

registerHooks(hooks: JestHookSubscriber) {
apply(hooks: JestHookSubscriber) {
hooks.testRunComplete(results => {
this._failedSnapshotTestPaths = getFailedSnapshotTests(results);
if (this._snapshotInteractiveMode.isActive()) {
@@ -33,13 +33,13 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
});
}

onData(key: string) {
onKey(key: string) {
if (this._snapshotInteractiveMode.isActive()) {
this._snapshotInteractiveMode.put(key);
}
}

showPrompt(
runInteractive(
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
): Promise<void> {
6 changes: 3 additions & 3 deletions packages/jest-cli/src/types.js
Original file line number Diff line number Diff line change
@@ -19,10 +19,10 @@ export type JestHooks = {
};

export type WatchPlugin = {
registerHooks?: (hooks: JestHookSubscriber) => void,
apply?: (hooks: JestHookSubscriber) => void,
getUsageData?: (globalConfig: GlobalConfig) => ?UsageData,
onData?: (value: string) => void,
showPrompt?: (
onKey?: (value: string) => void,
runInteractive?: (
globalConfig: GlobalConfig,
updateConfigAndRun: Function,
) => Promise<void | boolean>,
16 changes: 8 additions & 8 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
@@ -122,8 +122,8 @@ export default function watch(

watchPlugins.forEach((plugin: WatchPlugin) => {
const hookSubscriber = hooks.getSubscriber();
if (plugin.registerHooks) {
plugin.registerHooks(hookSubscriber);
if (plugin.apply) {
plugin.apply(hookSubscriber);
}
});

@@ -136,8 +136,8 @@ export default function watch(
stdout: outputStream,
});
const hookSubscriber = hooks.getSubscriber();
if (plugin.registerHooks) {
plugin.registerHooks(hookSubscriber);
if (plugin.apply) {
plugin.apply(hookSubscriber);
}
watchPlugins.push(plugin);
}
@@ -249,10 +249,10 @@ export default function watch(
return;
}

if (activePlugin != null && activePlugin.onData) {
if (activePlugin != null && activePlugin.onKey) {
// if a plugin is activate, Jest should let it handle keystrokes, so ignore
// them here
activePlugin.onData(key);
activePlugin.onKey(key);
return;
}

@@ -281,8 +281,8 @@ export default function watch(
// "activate" the plugin, which has jest ignore keystrokes so the plugin
// can handle them
activePlugin = matchingWatchPlugin;
if (activePlugin.showPrompt) {
activePlugin.showPrompt(globalConfig, updateConfigAndRun).then(
if (activePlugin.runInteractive) {
activePlugin.runInteractive(globalConfig, updateConfigAndRun).then(
shouldRerun => {
activePlugin = null;
if (shouldRerun) {