Skip to content

Commit

Permalink
feat: add jest-watch package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 27, 2018
1 parent d59017d commit 93af1bc
Show file tree
Hide file tree
Showing 38 changed files with 190 additions and 111 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Features

* `[jest-watch]` create new package `jest-watch` to ease custom watch plugin development ([#6318](https://github.com/facebook/jest/pull/6318))

### Fixes

* `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"jest-snapshot": "^23.0.1",
"jest-util": "^23.0.1",
"jest-validate": "^23.0.1",
"jest-watch": "^23.0.1",
"jest-worker": "^23.0.1",
"micromatch": "^2.3.11",
"node-notifier": "^5.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@
*/

import chalk from 'chalk';
import {KEYS} from '../constants';
import {KEYS} from 'jest-watch';
import SnapshotInteractiveMode from '../snapshot_interactive_mode';

jest.mock('../lib/terminal_utils', () => ({
getTerminalWidth: () => 80,
rightPad: () => {
'';
},
}));

jest.mock('ansi-escapes', () => ({
clearScreen: '[MOCK - eraseDown]',
cursorRestorePosition: '[MOCK - cursorRestorePosition]',
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import chalk from 'chalk';
import TestWatcher from '../test_watcher';
import JestHooks from '../jest_hooks';
import {KEYS} from '../constants';
import {JestHook, KEYS} from 'jest-watch';

const runJestMock = jest.fn();
const watchPluginPath = `${__dirname}/__fixtures__/watch_plugin`;
Expand Down Expand Up @@ -286,7 +285,7 @@ describe('Watch mode flows', () => {
expect(pipeMockCalls.slice(determiningTestsToRun + 1)).toMatchSnapshot();
});

it('allows WatchPlugins to hook into JestHooks', async () => {
it('allows WatchPlugins to hook into JestHook', async () => {
const apply = jest.fn();
const pluginPath = `${__dirname}/__fixtures__/plugin_path_register`;
jest.doMock(
Expand Down Expand Up @@ -530,7 +529,7 @@ describe('Watch mode flows', () => {
});

it('Pressing "t" reruns the tests in "test name pattern" mode', async () => {
const hooks = new JestHooks();
const hooks = new JestHook();

watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
runJestMock.mockReset();
Expand All @@ -549,7 +548,7 @@ describe('Watch mode flows', () => {
});

it('Pressing "p" reruns the tests in "filename pattern" mode', async () => {
const hooks = new JestHooks();
const hooks = new JestHook();

watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
runJestMock.mockReset();
Expand All @@ -568,7 +567,7 @@ describe('Watch mode flows', () => {
});

it('Can combine "p" and "t" filters', async () => {
const hooks = new JestHooks();
const hooks = new JestHook();

watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
runJestMock.mockReset();
Expand All @@ -592,7 +591,7 @@ describe('Watch mode flows', () => {
});

it('Pressing "u" reruns the tests in "update snapshot" mode', async () => {
const hooks = new JestHooks();
const hooks = new JestHook();

globalConfig.updateSnapshot = 'new';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

import chalk from 'chalk';
import {KEYS} from '../constants';
import {KEYS} from 'jest-watch';

const runJestMock = jest.fn();

Expand Down Expand Up @@ -79,10 +79,6 @@ jest.doMock(
},
);

jest.doMock('../lib/terminal_utils', () => ({
getTerminalWidth: () => terminalWidth,
}));

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

const nextTick = () => new Promise(res => process.nextTick(res));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

import chalk from 'chalk';
import {KEYS} from '../constants';
import {KEYS} from 'jest-watch';

const runJestMock = jest.fn();

Expand Down Expand Up @@ -93,10 +93,6 @@ jest.doMock(
},
);

jest.doMock('../lib/terminal_utils', () => ({
getTerminalWidth: () => terminalWidth,
}));

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

const globalConfig = {
Expand Down
27 changes: 0 additions & 27 deletions packages/jest-cli/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,6 @@ const isWindows = process.platform === 'win32';

export const CLEAR = isWindows ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H';
export const ARROW = ' \u203A ';
export const KEYS = {
A: 'a',
ARROW_DOWN: '\u001b[B',
ARROW_LEFT: '\u001b[D',
ARROW_RIGHT: '\u001b[C',
ARROW_UP: '\u001b[A',
BACKSPACE: isWindows
? Buffer.from('08', 'hex').toString()
: Buffer.from('7f', 'hex').toString(),
C: 'c',
CONTROL_C: '\u0003',
CONTROL_D: '\u0004',
ENTER: '\r',
ESCAPE: '\u001b',
F: 'f',
I: 'i',
O: 'o',
P: 'p',
Q: 'q',
QUESTION_MARK: '?',
R: 'r',
S: 's',
T: 't',
U: 'u',
W: 'w',
};

export const ICONS = {
failed: isWindows ? '\u00D7' : '\u2715',
pending: '\u25CB',
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-cli/src/lib/handle_deprecation_warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/

import chalk from 'chalk';

import {KEYS} from '../constants';
import {KEYS} from 'jest-watch';

export default (
pipe: stream$Writable | tty$WriteStream,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/plugins/quit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @flow
*/
import BaseWatchPlugin from '../base_watch_plugin';
import {BaseWatchPlugin} from 'jest-watch';

class QuitPlugin extends BaseWatchPlugin {
isInternal: true;
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-cli/src/plugins/test_name_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import BaseWatchPlugin from '../base_watch_plugin';
import {BaseWatchPlugin, Prompt} from 'jest-watch';
import TestNamePatternPrompt from '../test_name_pattern_prompt';
import activeFilters from '../lib/active_filters_message';
import Prompt from '../lib/Prompt';

class TestNamePatternPlugin extends BaseWatchPlugin {
_prompt: Prompt;
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-cli/src/plugins/test_path_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
*/

import type {GlobalConfig} from 'types/Config';
import BaseWatchPlugin from '../base_watch_plugin';
import {BaseWatchPlugin, Prompt} from 'jest-watch';
import TestPathPatternPrompt from '../test_path_pattern_prompt';
import activeFilters from '../lib/active_filters_message';
import Prompt from '../lib/Prompt';

class TestPathPatternPlugin extends BaseWatchPlugin {
_prompt: Prompt;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/plugins/update_snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import BaseWatchPlugin from '../base_watch_plugin';
import type {JestHookSubscriber} from '../jest_hooks';
import type {JestHookSubscriber} from 'types/JestHooks';
import {BaseWatchPlugin} from 'jest-watch';

class UpdateSnapshotsPlugin extends BaseWatchPlugin {
_hasSnapshotFailure: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/plugins/update_snapshots_interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*
* @flow
*/
import type {JestHookSubscriber} from '../jest_hooks';
import type {JestHookSubscriber} from 'types/JestHooks';
import type {GlobalConfig} from 'types/Config';
import type {AggregatedResult, AssertionLocation} from 'types/TestResult';
import BaseWatchPlugin from '../base_watch_plugin';
import {BaseWatchPlugin} from 'jest-watch';
import SnapshotInteractiveMode from '../snapshot_interactive_mode';

class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {Context} from 'types/Context';
import type {ChangedFilesPromise} from 'types/ChangedFiles';
import type {GlobalConfig} from 'types/Config';
import type {AggregatedResult} from 'types/TestResult';
import type {JestHookEmitter} from 'types/JestHooks';
import type TestWatcher from './test_watcher';

import micromatch from 'micromatch';
Expand All @@ -25,7 +26,7 @@ import TestScheduler from './test_scheduler';
import TestSequencer from './test_sequencer';
import {makeEmptyAggregatedTestResult} from './test_result_helpers';
import FailedTestsCache from './failed_tests_cache';
import JestHooks, {type JestHookEmitter} from './jest_hooks';
import {JestHook} from 'jest-watch';
import collectNodeHandles from './get_node_handles';

const setConfig = (contexts, newConfig) =>
Expand Down Expand Up @@ -115,7 +116,7 @@ export default (async function runJest({
globalConfig,
outputStream,
testWatcher,
jestHooks = new JestHooks().getEmitter(),
jestHooks = new JestHook().getEmitter(),
startRun,
changedFilesPromise,
onComplete,
Expand Down
10 changes: 6 additions & 4 deletions packages/jest-cli/src/snapshot_interactive_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

import type {AggregatedResult, AssertionLocation} from 'types/TestResult';

const chalk = require('chalk');
const ansiEscapes = require('ansi-escapes');
const {pluralize} = require('./reporters/utils');
const {KEYS, ARROW} = require('./constants');
import chalk from 'chalk';
import ansiEscapes from 'ansi-escapes';
import {KEYS} from 'jest-watch';

import {pluralize} from './reporters/utils';
import {ARROW} from './constants';

export default class SnapshotInteractiveMode {
_pipe: stream$Writable | tty$WriteStream;
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-cli/src/test_name_pattern_prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/

import type {TestResult} from 'types/TestResult';
import type {ScrollOptions} from './lib/scroll_list';
import type {ScrollOptions} from 'types/Watch';

import Prompt from './lib/Prompt';
import {
PatternPrompt,
Prompt,
printPatternCaret,
printRestoredPatternCaret,
} from './lib/pattern_mode_helpers';
import PatternPrompt from './pattern_prompt';
} from 'jest-watch';

export default class TestNamePatternPrompt extends PatternPrompt {
_cachedTestResults: Array<TestResult>;
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-cli/src/test_path_pattern_prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

import type {Context} from 'types/Context';
import type {Test} from 'types/TestRunner';
import type {ScrollOptions} from './lib/scroll_list';
import type {ScrollOptions} from 'types/Watch';
import type SearchSource from './search_source';

import Prompt from './lib/Prompt';
import {
PatternPrompt,
Prompt,
printPatternCaret,
printRestoredPatternCaret,
} from './lib/pattern_mode_helpers';
import PatternPrompt from './pattern_prompt';
} from 'jest-watch';

type SearchSources = Array<{|
context: Context,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import type {JestHookSubscriber} from './jest_hooks';
import type {JestHookSubscriber} from 'types/JestHooks';

export type UsageData = {
key: string,
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 @@ -26,8 +26,8 @@ import updateGlobalConfig from './lib/update_global_config';
import SearchSource from './search_source';
import TestWatcher from './test_watcher';
import FailedTestsCache from './failed_tests_cache';
import {KEYS, CLEAR} from './constants';
import JestHooks from './jest_hooks';
import {CLEAR} from './constants';
import {KEYS, JestHook} from 'jest-watch';
import TestPathPatternPlugin from './plugins/test_path_pattern';
import TestNamePatternPlugin from './plugins/test_name_pattern';
import UpdateSnapshotsPlugin from './plugins/update_snapshots';
Expand Down Expand Up @@ -55,7 +55,7 @@ export default function watch(
outputStream: stream$Writable | tty$WriteStream,
hasteMapInstances: Array<HasteMap>,
stdin?: stream$Readable | tty$ReadStream = process.stdin,
hooks?: JestHooks = new JestHooks(),
hooks?: JestHook = new JestHook(),
): Promise<void> {
// `globalConfig` will be constantly updated and reassigned as a result of
// watch mode interactions.
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-watch/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
20 changes: 20 additions & 0 deletions packages/jest-watch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "jest-watch",
"description": "Delightful JavaScript Testing.",
"version": "23.0.1",
"main": "build/index.js",
"dependencies": {
"ansi-escapes": "^3.0.0",
"chalk": "^2.0.1",
"string-length": "^2.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest"
},
"bugs": {
"url": "https://github.com/facebook/jest/issues"
},
"homepage": "http://facebook.github.io/jest/",
"license": "MIT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import type {JestHookSubscriber} from './jest_hooks';
import type {JestHookSubscriber} from 'types/JestHooks';
import type {WatchPlugin, UsageData} from './types';

class BaseWatchPlugin implements WatchPlugin {
Expand Down
Loading

0 comments on commit 93af1bc

Please sign in to comment.