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

Allow watch plugin to be configured #6603

Merged
merged 7 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-cli]` Allow watch plugin to be configured ([#6603](https://github.com/facebook/jest/pull/6603))
- `[jest-snapshot]` Introduce `toMatchInlineSnapshot` and `toThrowErrorMatchingInlineSnapshot` matchers ([#6380](https://github.com/facebook/jest/pull/6380))

### Chore & Maintenance
Expand Down
15 changes: 15 additions & 0 deletions packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ Watch Usage
]
`;

exports[`Watch mode flows allows WatchPlugins to be configured 1`] = `
Array [
"
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press k to configured prompt.
Copy link
Member

Choose a reason for hiding this comment

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

reads weird, but doesn't matter 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, let me change it to Press k to filter with a custom prompt

› Press Enter to trigger a test run.
",
]
`;

exports[`Watch mode flows allows WatchPlugins to override internal plugins 1`] = `
Array [
"
Expand Down
58 changes: 51 additions & 7 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Watch mode flows', () => {
await watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [watchPluginPath],
watchPlugins: [{config: {}, path: watchPluginPath}],
}),
contexts,
pipe,
Expand All @@ -195,7 +195,10 @@ describe('Watch mode flows', () => {
ci_watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [watchPlugin2Path, watchPluginPath],
watchPlugins: [
{config: {}, path: watchPluginPath},
{config: {}, path: watchPlugin2Path},
],
}),
contexts,
pipe,
Expand Down Expand Up @@ -302,7 +305,7 @@ describe('Watch mode flows', () => {
watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
watchPlugins: [{config: {}, path: pluginPath}],
}),
contexts,
pipe,
Expand Down Expand Up @@ -338,7 +341,7 @@ describe('Watch mode flows', () => {
watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
watchPlugins: [{config: {}, path: pluginPath}],
}),
contexts,
pipe,
Expand All @@ -356,6 +359,44 @@ describe('Watch mode flows', () => {
expect(run).toHaveBeenCalled();
});

it('allows WatchPlugins to be configured', async () => {
const pluginPath = `${__dirname}/__fixtures__/plugin_path_with_config`;
jest.doMock(
pluginPath,
() =>
class WatchPlugin {
constructor({config}) {
this._key = config.key;
this._prompt = config.prompt;
}
onKey() {}
run() {}
getUsageInfo() {
return {
key: this._key || 'z',
prompt: this._prompt || 'default prompt',
};
}
},
{virtual: true},
);

watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [
{config: {key: 'k', prompt: 'configured prompt'}, path: pluginPath},
],
}),
contexts,
pipe,
hasteMapInstances,
stdin,
);

expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot();
});

it('allows WatchPlugins to hook into file system changes', async () => {
const onFileChange = jest.fn();
const pluginPath = `${__dirname}/__fixtures__/plugin_path_fs_change`;
Expand All @@ -373,7 +414,7 @@ describe('Watch mode flows', () => {
watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
watchPlugins: [{config: {}, path: pluginPath}],
}),
contexts,
pipe,
Expand Down Expand Up @@ -414,7 +455,7 @@ describe('Watch mode flows', () => {
watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
watchPlugins: [{config: {}, path: pluginPath}],
}),
contexts,
pipe,
Expand Down Expand Up @@ -474,7 +515,10 @@ describe('Watch mode flows', () => {
watch(
Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath, pluginPath2],
watchPlugins: [
{config: {}, path: pluginPath},
{config: {}, path: pluginPath2},
],
}),
contexts,
pipe,
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 @@ -106,7 +106,6 @@ export default function watch(
const watchPlugins: Array<WatchPlugin> = INTERNAL_PLUGINS.map(
InternalPlugin => new InternalPlugin({stdin, stdout: outputStream}),
);

watchPlugins.forEach((plugin: WatchPlugin) => {
const hookSubscriber = hooks.getSubscriber();
if (plugin.apply) {
Expand All @@ -115,10 +114,11 @@ export default function watch(
});

if (globalConfig.watchPlugins != null) {
for (const pluginModulePath of globalConfig.watchPlugins) {
for (const pluginWithConfig of globalConfig.watchPlugins) {
// $FlowFixMe dynamic require
const ThirdPartyPlugin = require(pluginModulePath);
const ThirdPartyPlugin = require(pluginWithConfig.path);
const plugin: WatchPlugin = new ThirdPartyPlugin({
config: pluginWithConfig.config,
stdin,
stdout: outputStream,
});
Expand Down
28 changes: 21 additions & 7 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,27 @@ export default function normalize(options: InitialOptions, argv: Argv) {
value = options[key];
break;
case 'watchPlugins':
value = (options[key] || []).map(watchPlugin =>
resolve(newOptions.resolver, {
filePath: watchPlugin,
key,
rootDir: options.rootDir,
}),
);
value = (options[key] || []).map(watchPlugin => {
if (typeof watchPlugin === 'string') {
return {
config: {},
path: resolve(newOptions.resolver, {
filePath: watchPlugin,
key,
rootDir: options.rootDir,
}),
};
} else {
return {
config: watchPlugin[1] || {},
path: resolve(newOptions.resolver, {
filePath: watchPlugin[0],
key,
rootDir: options.rootDir,
}),
};
}
});
break;
}
newOptions[key] = value;
Expand Down
4 changes: 2 additions & 2 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export type InitialOptions = {
watch?: boolean,
watchAll?: boolean,
watchman?: boolean,
watchPlugins?: Array<string>,
watchPlugins?: Array<string | [string, Object]>,
};

export type SnapshotUpdateState = 'all' | 'new' | 'none';
Expand Down Expand Up @@ -235,7 +235,7 @@ export type GlobalConfig = {|
watch: boolean,
watchAll: boolean,
watchman: boolean,
watchPlugins: ?Array<string>,
watchPlugins: ?Array<string | [string, Object]>,
|};

export type ProjectConfig = {|
Expand Down