From 734ef2bd3afe4a3379a3383eec249a2e1001646d Mon Sep 17 00:00:00 2001 From: Jeremy Gillick Date: Sun, 2 Jul 2023 10:15:34 -0700 Subject: [PATCH] Add clearTerminal setting (#1014) * Add clearTerminal setting * Rename clearTerminal to autoClearTerminal. Auto clear on watch. * Unit tests * Update README * Fix tests * Revert test removal --- README.md | 1 + package.json | 5 +++++ src/JestExt/helper.ts | 1 + src/JestExt/output-terminal.ts | 7 +++++++ src/Settings/index.ts | 1 + src/test-provider/test-item-data.ts | 6 ++++++ tests/JestExt/helper.test.ts | 1 + tests/JestExt/outpt-terminal.test.ts | 6 ++++++ tests/test-provider/test-helper.ts | 2 +- tests/test-provider/test-item-data.test.ts | 24 ++++++++++++++++++++++ 10 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c2629236..bf5481e04 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,7 @@ useDashedArgs| Determine if to use dashed arguments for jest processes |undefine |debugMode|Enable debug mode to diagnose plugin issues. (see developer console)|false|`"jest.debugMode": true`| |disabledWorkspaceFolders 💼|Disabled workspace folders names in multi-root environment|[]|`"jest.disabledWorkspaceFolders": ["package-a", "package-b"]`| |[autoRevealOutput](#autoRevealOutput)|Determine when to show test output|"on-run"|`"jest.autoRevealOutput": "on-exec-error"`| +|autoClearTerminal|Clear the terminal output at the start of any new test run.|false|`"jest.autoClearTerminal": true`| #### Details ##### jestCommandLine diff --git a/package.json b/package.json index 7baefcb76..e3cd10ac7 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,11 @@ "type": "string", "scope": "resource" }, + "jest.autoClearTerminal": { + "description": "Clear the terminal output at the start of any new test run.", + "type": "boolean", + "scope": "resource" + }, "jest.rootPath": { "description": "The path to your frontend src folder", "type": "string", diff --git a/src/JestExt/helper.ts b/src/JestExt/helper.ts index 0c02cb59e..cf7a8f5ad 100644 --- a/src/JestExt/helper.ts +++ b/src/JestExt/helper.ts @@ -111,6 +111,7 @@ export const getExtensionResourceSettings = ( return { jestCommandLine: getSetting('jestCommandLine'), + autoClearTerminal: getSetting('autoClearTerminal') ?? false, rootPath: toAbsoluteRootPath(workspaceFolder, getSetting('rootPath')), showCoverageOnLoad: getSetting('showCoverageOnLoad') ?? false, coverageFormatter: getSetting('coverageFormatter') ?? 'DefaultFormatter', diff --git a/src/JestExt/output-terminal.ts b/src/JestExt/output-terminal.ts index 60eaaa6af..df2295c91 100644 --- a/src/JestExt/output-terminal.ts +++ b/src/JestExt/output-terminal.ts @@ -133,6 +133,13 @@ export class ExtOutputTerminal implements JestExtOutput { this.writeEmitter.dispose(); this._terminal?.dispose(); } + + /** + * Clear the terminal + */ + clear(): void { + this.write('\x1bc'); + } } export class JestOutputTerminal extends ExtOutputTerminal { constructor(workspaceName: string, visible?: boolean) { diff --git a/src/Settings/index.ts b/src/Settings/index.ts index b67c03d77..533ad290e 100644 --- a/src/Settings/index.ts +++ b/src/Settings/index.ts @@ -41,6 +41,7 @@ export type MonitorLongRun = 'off' | number; export type AutoRevealOutputType = 'on-run' | 'on-exec-error' | 'off'; export interface PluginResourceSettings { jestCommandLine?: string; + autoClearTerminal?: boolean; rootPath: string; showCoverageOnLoad: boolean; coverageFormatter: string; diff --git a/src/test-provider/test-item-data.ts b/src/test-provider/test-item-data.ts index edb7c3a02..a1bd65396 100644 --- a/src/test-provider/test-item-data.ts +++ b/src/test-provider/test-item-data.ts @@ -412,6 +412,9 @@ export class WorkspaceRoot extends TestItemDataBase { run = this.createRunForEvent(event); this.deepItemState(run.item, run.enqueued); } + if (this.context.ext.settings.autoClearTerminal === true) { + this.context.output.clear(); + } break; } @@ -426,6 +429,9 @@ export class WorkspaceRoot extends TestItemDataBase { case 'start': { run = run ?? this.createRunForEvent(event); this.deepItemState(run.item, run.started); + if (this.context.ext.settings.autoClearTerminal === true) { + this.context.output.clear(); + } this.runLog('started'); break; } diff --git a/tests/JestExt/helper.test.ts b/tests/JestExt/helper.test.ts index 84044f061..b6a330e74 100644 --- a/tests/JestExt/helper.test.ts +++ b/tests/JestExt/helper.test.ts @@ -169,6 +169,7 @@ describe('getExtensionResourceSettings()', () => { showCoverageOnLoad: false, debugMode: false, coverageColors: null, + autoClearTerminal: false, autoRun: expect.objectContaining({ config: { watch: true } }), testExplorer: {}, monitorLongRun: 60000, diff --git a/tests/JestExt/outpt-terminal.test.ts b/tests/JestExt/outpt-terminal.test.ts index 956b7b215..41942f301 100644 --- a/tests/JestExt/outpt-terminal.test.ts +++ b/tests/JestExt/outpt-terminal.test.ts @@ -125,6 +125,12 @@ describe('JestOutputTerminal', () => { expect(mockTerminal.dispose).toHaveBeenCalled(); expect(mockEmitter.dispose).not.toHaveBeenCalled(); }); + it('can clear the terminal', () => { + const output = new JestOutputTerminal('a'); + output.write = jest.fn().mockImplementation(() => 'test'); + output.clear(); + expect(output.write).toHaveBeenCalledWith('\x1bc'); + }); describe('can write output with options', () => { it.each` case | text | options diff --git a/tests/test-provider/test-helper.ts b/tests/test-provider/test-helper.ts index 6ac02f5ff..6407a278d 100644 --- a/tests/test-provider/test-helper.ts +++ b/tests/test-provider/test-helper.ts @@ -41,7 +41,7 @@ export const mockExtExplorerContext = (wsName = 'ws-1', override: any = {}): any debugTests: jest.fn(), sessionEvents: mockJestExtEvents(), settings: { testExplorer: { enabled: true }, autoRun: {} }, - output: { write: jest.fn(), dispose: jest.fn() }, + output: { write: jest.fn(), dispose: jest.fn(), clear: jest.fn() }, ...override, }; }; diff --git a/tests/test-provider/test-item-data.test.ts b/tests/test-provider/test-item-data.test.ts index 4ee2ddf8e..1c53bbd09 100644 --- a/tests/test-provider/test-item-data.test.ts +++ b/tests/test-provider/test-item-data.test.ts @@ -1171,6 +1171,30 @@ describe('test-item-data', () => { expect.stringContaining(coloredText) ); }); + describe('optionally clear terminal on start & schedule', () => { + let env; + beforeEach(() => { + env = setupTestEnv(); + }); + it.each([{ type: 'scheduled' }, { type: 'start' }])( + '$type: clear when autoClearTerminal is true', + ({ type }) => { + context.ext.settings = { autoClearTerminal: true }; + const process = mockScheduleProcess(context); + env.onRunEvent({ type, process }); + expect(context.output.clear).toHaveBeenCalled(); + } + ); + it.each([{ type: 'scheduled' }, { type: 'start' }])( + '$type: do not clear when when autoClearTerminal is false', + ({ type }) => { + context.ext.settings = { autoClearTerminal: false }; + const process = mockScheduleProcess(context); + env.onRunEvent({ type, process }); + expect(context.output.clear).not.toHaveBeenCalled(); + } + ); + }); describe('handle run event to set item status and show output', () => { let env; beforeEach(() => {