From abca0fe6c1d9fed884e4dc8bb911b3497db506ee Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 22 Mar 2022 09:40:25 +0200 Subject: [PATCH] refactor(@jest/core, jest-watcher)!: rework and clean up `PatternPrompt` and its derived classes (#12591) --- CHANGELOG.md | 1 + .../jest-core/src/TestNamePatternPrompt.ts | 16 +++----------- .../jest-core/src/TestPathPatternPrompt.ts | 21 +++---------------- packages/jest-watcher/src/PatternPrompt.ts | 15 ++++++------- 4 files changed, 13 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f13f52c65058..273ced1968e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - `[jest-runtime]` Calling `jest.resetModules` function will clear FS and transform cache ([#12531](https://github.com/facebook/jest/pull/12531)) - `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) - `[jest-test-result]` Add duration property to JSON test output ([#12518](https://github.com/facebook/jest/pull/12518)) +- `[jest-watcher]` [**BREAKING**] Make `PatternPrompt` class to take `entityName` as third constructor parameter instead of `this._entityName` ([#12591](https://github.com/facebook/jest/pull/12591)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) - `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402)) diff --git a/packages/jest-core/src/TestNamePatternPrompt.ts b/packages/jest-core/src/TestNamePatternPrompt.ts index f69312c05c0a..426a8bc45956 100644 --- a/packages/jest-core/src/TestNamePatternPrompt.ts +++ b/packages/jest-core/src/TestNamePatternPrompt.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import type {TestResult} from '@jest/test-result'; import { PatternPrompt, Prompt, @@ -14,28 +13,19 @@ import { printRestoredPatternCaret, } from 'jest-watcher'; -// TODO: Make underscored props `private` export default class TestNamePatternPrompt extends PatternPrompt { - _cachedTestResults: Array; - constructor(pipe: NodeJS.WritableStream, prompt: Prompt) { - super(pipe, prompt); - this._entityName = 'tests'; - this._cachedTestResults = []; + super(pipe, prompt, 'tests'); } - _onChange(pattern: string, options: ScrollOptions): void { + protected _onChange(pattern: string, options: ScrollOptions): void { super._onChange(pattern, options); this._printPrompt(pattern); } - _printPrompt(pattern: string): void { + private _printPrompt(pattern: string): void { const pipe = this._pipe; printPatternCaret(pattern, pipe); printRestoredPatternCaret(pattern, this._currentUsageRows, pipe); } - - updateCachedTestResults(testResults: Array = []): void { - this._cachedTestResults = testResults; - } } diff --git a/packages/jest-core/src/TestPathPatternPrompt.ts b/packages/jest-core/src/TestPathPatternPrompt.ts index cea3f46514c8..6c9d948da346 100644 --- a/packages/jest-core/src/TestPathPatternPrompt.ts +++ b/packages/jest-core/src/TestPathPatternPrompt.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import type {Context} from 'jest-runtime'; import { PatternPrompt, Prompt, @@ -13,34 +12,20 @@ import { printPatternCaret, printRestoredPatternCaret, } from 'jest-watcher'; -import type SearchSource from './SearchSource'; -type SearchSources = Array<{ - context: Context; - searchSource: SearchSource; -}>; - -// TODO: Make underscored props `private` export default class TestPathPatternPrompt extends PatternPrompt { - _searchSources?: SearchSources; - constructor(pipe: NodeJS.WritableStream, prompt: Prompt) { - super(pipe, prompt); - this._entityName = 'filenames'; + super(pipe, prompt, 'filenames'); } - _onChange(pattern: string, options: ScrollOptions): void { + protected _onChange(pattern: string, options: ScrollOptions): void { super._onChange(pattern, options); this._printPrompt(pattern); } - _printPrompt(pattern: string): void { + private _printPrompt(pattern: string): void { const pipe = this._pipe; printPatternCaret(pattern, pipe); printRestoredPatternCaret(pattern, this._currentUsageRows, pipe); } - - updateSearchSources(searchSources: SearchSources): void { - this._searchSources = searchSources; - } } diff --git a/packages/jest-watcher/src/PatternPrompt.ts b/packages/jest-watcher/src/PatternPrompt.ts index a0267fcc98cf..aa201dffa7e2 100644 --- a/packages/jest-watcher/src/PatternPrompt.ts +++ b/packages/jest-watcher/src/PatternPrompt.ts @@ -22,17 +22,14 @@ const usage = (entity: string) => const usageRows = usage('').split('\n').length; -export default class PatternPrompt { - protected _pipe: NodeJS.WritableStream; - protected _prompt: Prompt; - protected _entityName: string; +export default abstract class PatternPrompt { protected _currentUsageRows: number; - constructor(pipe: NodeJS.WritableStream, prompt: Prompt) { - // TODO: Should come in the constructor - this._entityName = ''; - this._pipe = pipe; - this._prompt = prompt; + constructor( + protected _pipe: NodeJS.WritableStream, + protected _prompt: Prompt, + protected _entityName = '', + ) { this._currentUsageRows = usageRows; }