Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
396cd18
Start adding tapable
rogeliog Jan 17, 2018
c04edc7
Use test_path_pattern as a plugin
rogeliog Jan 19, 2018
1f74aa1
Use test_name_pattern as a plugin
rogeliog Jan 19, 2018
03b53b1
Use quit as a plugin
rogeliog Jan 19, 2018
1aeafb8
Fix test interruption
rogeliog Jan 19, 2018
b40f604
Use update snapshot as a plugin
rogeliog Jan 19, 2018
6e97a6e
Use update snapshot interactive as a plugin
rogeliog Jan 25, 2018
ef14ade
Change API to use a class instance
rogeliog Jan 26, 2018
8b999ed
Merge branch 'master' into simpler-plugins
rogeliog Jan 26, 2018
2604965
A bit of clean up and make tests pass
rogeliog Jan 26, 2018
ffc85f1
Change plugin implementation to not use tapable
rogeliog Feb 2, 2018
053d9e4
Better sorting implementation
rogeliog Feb 2, 2018
2036be9
Add back third party plugin functionality
rogeliog Feb 2, 2018
40c4265
Fix flow
rogeliog Feb 2, 2018
44c82ba
Merge branch 'master' into simpler-plugins
rogeliog Feb 2, 2018
7767b71
Fix ESLint
rogeliog Feb 2, 2018
2b084f8
Reset file to state of master
rogeliog Feb 2, 2018
58f1717
Update failing snapshot
rogeliog Feb 2, 2018
13f08e5
Merge branch 'master' into simpler-plugins
rogeliog Feb 6, 2018
d4ecb92
Remove hasSnapshotFailure and hasSnapshotFailureInteractive
rogeliog Feb 6, 2018
8f6513e
Async await for showPrompt and clear active plugin on file change
rogeliog Feb 6, 2018
401e44b
Fix snapshot failure
rogeliog Feb 6, 2018
9ac7d94
Reenable tests
rogeliog Feb 6, 2018
25e0c4b
Implement shouldRunTestSuite
rogeliog Feb 6, 2018
4a68617
Add changelog
rogeliog Feb 6, 2018
3dc8c06
Clean up watch.js a bit
rogeliog Feb 6, 2018
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
Use test_path_pattern as a plugin
  • Loading branch information
rogeliog committed Jan 19, 2018
commit c04edc732760e197a6087cdbb052cd9e0e9e362f
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

// prettier-ignore
test('escape strings', () => expect('one: \\\'').toMatchSnapshot());
test('escape strings two', () => expect('two: \'"').toMatchSnapshot());
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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 p to filter by a filename regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
",
Expand All @@ -28,8 +28,8 @@ 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 p to filter by a filename regex pattern.
› Press s to do nothing.
› Press u to do something else.
› Press q to quit watch mode.
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jest.doMock(
jest.doMock(
watchPluginPath,
() => ({
enter: jest.fn(),
apply: jest.fn(),
key: 's'.codePointAt(0),
prompt: 'do nothing',
}),
Expand All @@ -46,7 +46,7 @@ jest.doMock(
jest.doMock(
watchPlugin2Path,
() => ({
enter: jest.fn(),
apply: jest.fn(),
key: 'u'.codePointAt(0),
prompt: 'do something else',
}),
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('Watch mode flows', () => {
expect(pipeMockCalls.slice(determiningTestsToRun + 1)).toMatchSnapshot();
});

it('triggers enter on a WatchPlugin when its key is pressed', () => {
xit('triggers enter on a WatchPlugin when its key is pressed', () => {
const plugin = require(watchPluginPath);

watch(
Expand All @@ -191,10 +191,10 @@ describe('Watch mode flows', () => {

stdin.emit(plugin.key.toString(16));

expect(plugin.enter).toHaveBeenCalled();
expect(plugin.apply).toHaveBeenCalled();
});

it('prevents Jest from handling keys when active and returns control when end is called', () => {
xit('prevents Jest from handling keys when active and returns control when end is called', () => {
Copy link
Member

Choose a reason for hiding this comment

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

seems like something we'd want, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, this is one of the pending items that I have at the top.

const plugin = require(watchPluginPath);
const plugin2 = require(watchPlugin2Path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ jest.doMock('../lib/terminal_utils', () => ({
getTerminalWidth: () => terminalWidth,
}));

const nextTick = () => new Promise(res => process.nextTick(res));

const watch = require('../watch').default;

const toHex = char => Number(char.charCodeAt(0)).toString(16);
Expand All @@ -105,7 +107,7 @@ describe('Watch mode flows', () => {
stdin = new MockStdin();
});

it('Pressing "P" enters pattern mode', () => {
it('Pressing "P" enters pattern mode', async () => {
contexts[0].config = {rootDir: ''};
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);

Expand All @@ -129,6 +131,7 @@ describe('Watch mode flows', () => {
// Runs Jest again
runJestMock.mockReset();
stdin.emit(KEYS.ENTER);
await nextTick();
expect(runJestMock).toBeCalled();

// globalConfig is updated with the current pattern
Expand All @@ -142,25 +145,25 @@ describe('Watch mode flows', () => {
});
});

it('Pressing "c" clears the filters', () => {
it('Pressing "c" clears the filters', async () => {
contexts[0].config = {rootDir: ''};
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);

stdin.emit(KEYS.P);

['p', '.', '*', '1', '0']
.map(toHex)
.concat(KEYS.ENTER)
.forEach(key => stdin.emit(key));

await nextTick();

stdin.emit(KEYS.T);
['t', 'e', 's', 't']
.map(toHex)
.concat(KEYS.ENTER)
.forEach(key => stdin.emit(key));

stdin.emit(KEYS.C);

pipe.write.mockReset();
stdin.emit(KEYS.P);
expect(pipe.write.mock.calls.join('\n')).toMatchSnapshot();
Expand Down
37 changes: 37 additions & 0 deletions packages/jest-cli/src/lib/active_filters_message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import chalk from 'chalk';

const activeFilters = (
globalConfig: GlobalConfig,
delimiter: string = '\n',
) => {
const {testNamePattern, testPathPattern} = globalConfig;
if (testNamePattern || testPathPattern) {
const filters = [
testPathPattern
? chalk.dim('filename ') + chalk.yellow('/' + testPathPattern + '/')
: null,
testNamePattern
? chalk.dim('test name ') + chalk.yellow('/' + testNamePattern + '/')
: null,
]
.filter(f => !!f)
Copy link
Member

Choose a reason for hiding this comment

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

.filter(Boolean)? Or just remove the double bang, not really needed (as you check against null)

Copy link
Contributor Author

@rogeliog rogeliog Feb 6, 2018

Choose a reason for hiding this comment

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

Sorry, I just moved that from watch.js, changing it now

.join(', ');

const messages = ['\n' + chalk.bold('Active Filters: ') + filters];
Copy link
Member

Choose a reason for hiding this comment

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

Why in an array? Did you mean to concat with filters instead of joining on line 27?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this doesn't seem to be needed, same as above just moved to from watch.js


return messages.filter(message => !!message).join(delimiter);
Copy link
Member

Choose a reason for hiding this comment

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

this is still weird - messages will always be an array of size 1

}

return '';
};

export default activeFilters;
7 changes: 6 additions & 1 deletion packages/jest-cli/src/lib/watch_plugin_registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class WatchPluginRegistry {

loadPluginPath(pluginModulePath: string) {
// $FlowFixMe dynamic require
const maybePlugin = require(pluginModulePath).default;
let maybePlugin = require(pluginModulePath);

// Since we're loading the module from a dynamic path, assert its shape
// before assuming it's a valid watch plugin.
Expand All @@ -35,6 +35,11 @@ export default class WatchPluginRegistry {
`Jest watch plugin ${pluginModulePath} must be an ES Module or export an object`,
);
}

if (getType(maybePlugin.default) === 'object') {
maybePlugin = maybePlugin.default;
}

if (getType(maybePlugin.key) !== 'number') {
throw new Error(
`Jest watch plugin ${pluginModulePath} must export 'key' as a number`,
Expand Down
44 changes: 39 additions & 5 deletions packages/jest-cli/src/plugins/test_path_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,47 @@
*
* @flow
*/

import type {WatchPlugin} from '../types';
import TestPathPatternPrompt from '../test_path_pattern_prompt';
import activeFilters from '../lib/active_filters_message';
import Prompt from '../lib/Prompt';

const PLUGIN_NAME = 'test-path-pattern';
export default {
apply: () => {},
key: 'x'.codePointAt(0),
const testPathPatternPlugin: WatchPlugin = {
apply: (jestHooks, {stdin, stdout}) => {
const showPrompt = globalConfig => {
return new Promise((res, rej) => {
let exited = false;
const prompt = new Prompt();
const testPathPatternPrompt = new TestPathPatternPrompt(stdout, prompt);

stdin.on('data', (value, ...args) => {
if (!exited) {
prompt.put(value);
}
});

testPathPatternPrompt.run(
(value: string) => {
exited = true;
res({testPathPattern: value});
},
() => {
exited = true;
rej();
},
{
header: activeFilters(globalConfig),
},
);
});
};

jestHooks.showPrompt.tapPromise(PLUGIN_NAME, showPrompt);
},
key: 'p'.codePointAt(0),
name: PLUGIN_NAME,
prompt: 'adsad',
prompt: 'filter by a filename regex pattern',
};

export default testPathPatternPlugin;
6 changes: 4 additions & 2 deletions packages/jest-cli/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
* @flow
*/

import type {GlobalConfig} from 'types/Config';
export type JestHooks = {
showPrompt: any,
};

export type WatchPlugin = {
key: number,
name: string,
prompt: string,
apply: (
jestHooks: any,
jestHooks: JestHooks,
{
stdin: stream$Readable | tty$ReadStream,
stdout: stream$Writable | tty$WriteStream,
Expand Down
57 changes: 26 additions & 31 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import SearchSource from './search_source';
import SnapshotInteractiveMode from './snapshot_interactive_mode';
import TestWatcher from './test_watcher';
import Prompt from './lib/Prompt';
import TestPathPatternPrompt from './test_path_pattern_prompt';
import TestNamePatternPrompt from './test_name_pattern_prompt';
import FailedTestsCache from './failed_tests_cache';
import WatchPluginRegistry from './lib/watch_plugin_registry';
Expand Down Expand Up @@ -81,9 +80,15 @@ export default function watch(
}
}

watchPlugins.getPluginsOrderedByKey().forEach(plugin => {
plugin.apply(hooks, {
stdin,
stdout: outputStream,
});
});

const failedTestsCache = new FailedTestsCache();
const prompt = new Prompt();
const testPathPatternPrompt = new TestPathPatternPrompt(outputStream, prompt);
const testNamePatternPrompt = new TestNamePatternPrompt(outputStream, prompt);
const snapshotInteractiveMode = new SnapshotInteractiveMode(outputStream);
let failedSnapshotTestPaths = [];
Expand All @@ -97,8 +102,6 @@ export default function watch(
let shouldDisplayWatchUsage = true;
let isWatchUsageDisplayed = false;

testPathPatternPrompt.updateSearchSources(searchSources);

hasteMapInstances.forEach((hasteMapInstance, index) => {
hasteMapInstance.on('change', ({eventsQueue, hasteFS, moduleMap}) => {
const validPaths = eventsQueue.filter(({filePath}) => {
Expand All @@ -119,7 +122,6 @@ export default function watch(
context,
searchSource: new SearchSource(context),
};
testPathPatternPrompt.updateSearchSources(searchSources);
startRun(globalConfig);
}
});
Expand Down Expand Up @@ -217,12 +219,13 @@ export default function watch(
}

// Abort test run
const pluginKeys = watchPlugins.getPluginsOrderedByKey().map(p => p.key);
if (
isRunning &&
testWatcher &&
[KEYS.Q, KEYS.ENTER, KEYS.A, KEYS.O, KEYS.P, KEYS.T, KEYS.F].indexOf(
key,
) !== -1
[KEYS.Q, KEYS.ENTER, KEYS.A, KEYS.O, KEYS.T, KEYS.F]
.concat(pluginKeys)
.indexOf(key) !== -1
) {
testWatcher.setState({interrupted: true});
return;
Expand All @@ -235,10 +238,21 @@ export default function watch(
// "activate" the plugin, which has jest ignore keystrokes so the plugin
// can handle them
activePlugin = matchingWatchPlugin;
activePlugin.enter(
globalConfig,
// end callback -- returns control to jest to handle keystrokes
() => (activePlugin = null),
hooks.showPrompt.promise(globalConfig).then(
({testNamePattern, testPathPattern} = {}) => {
activePlugin = null;
globalConfig = updateGlobalConfig(globalConfig, {
mode: 'watch',
testNamePattern,
testPathPattern: replacePathSepForRegex(testPathPattern || ''),
});

startRun(globalConfig);
},
() => {
activePlugin = null;
onCancelPatternPrompt();
},
);
}

Expand Down Expand Up @@ -312,21 +326,6 @@ export default function watch(
});
startRun(globalConfig);
break;
case KEYS.P:
testPathPatternPrompt.run(
testPathPattern => {
globalConfig = updateGlobalConfig(globalConfig, {
mode: 'watch',
testNamePattern: '',
testPathPattern: replacePathSepForRegex(testPathPattern),
});

startRun(globalConfig);
},
onCancelPatternPrompt,
{header: activeFilters(globalConfig)},
);
break;
case KEYS.T:
testNamePatternPrompt.run(
testNamePattern => {
Expand Down Expand Up @@ -443,10 +442,6 @@ const usage = (
chalk.dim(' to update failing snapshots interactively.')
: null,

chalk.dim(' \u203A Press ') +
'p' +
chalk.dim(' to filter by a filename regex pattern.'),

chalk.dim(' \u203A Press ') +
't' +
chalk.dim(' to filter by a test name regex pattern.'),
Expand Down