Skip to content

Commit

Permalink
feat: Add DEC mode 2026 support (Synchronized Output) to Jest
Browse files Browse the repository at this point in the history
This commit introduces support for DEC private mode 2026, also known as Synchronized Output, to DefaultReporter. The Synchronized Output mode is a terminal feature that helps mitigate screen tearing effects that can occur when the terminal is rendering output while the application is still writing to the screen.

Two new methods have been added to the DefaultReporter:

- `__beginSynchronizedUpdate`: This method sends the control sequence to enable Synchronized Output mode to the terminal.
- `__endSynchronizedUpdate`: This method sends the control sequence to disable Synchronized Output mode to the terminal.

These methods are called before and after the reporter updates the status, respectively. By doing this, we ensure that the terminal renders a consistent state of the screen for each status update, even if we're writing to the screen frequently.

Read more: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
  • Loading branch information
haze committed Apr 4, 2024
1 parent 6c49a41 commit f3ae7aa
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/jest-reporters/src/DefaultReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export default class DefaultReporter extends BaseReporter {
this.__wrapStdio(process.stdout);
this.__wrapStdio(process.stderr);
this._status.onChange(() => {
this.__beginSynchronizedUpdate();
this.__clearStatus();
this.__printStatus();
this.__endSynchronizedUpdate();
});
}

Expand All @@ -69,11 +71,13 @@ export default class DefaultReporter extends BaseReporter {
buffer = [];

// This is to avoid conflicts between random output and status text
this.__beginSynchronizedUpdate();
this.__clearStatus();
if (string) {
write(string);
}
this.__printStatus();
this.__endSynchronizedUpdate();

this._bufferedOutput.delete(flushBufferedOutput);
};
Expand Down Expand Up @@ -120,6 +124,26 @@ export default class DefaultReporter extends BaseReporter {
}
}

protected __beginSynchronizedUpdate(): void {
if (isInteractive) {
if (this._globalConfig.useStderr) {
this._err('\x1b[?2026h');

Check failure on line 130 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use uppercase characters for the value of the escape sequence

Check failure on line 130 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use Unicode escapes instead of hexadecimal escapes
} else {
this._out('\x1b[?2026h');

Check failure on line 132 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use uppercase characters for the value of the escape sequence

Check failure on line 132 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use Unicode escapes instead of hexadecimal escapes
}
}
}

protected __endSynchronizedUpdate(): void {
if (isInteractive) {
if (this._globalConfig.useStderr) {
this._err('\x1b[?2026l');

Check failure on line 140 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use uppercase characters for the value of the escape sequence

Check failure on line 140 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use Unicode escapes instead of hexadecimal escapes
} else {
this._out('\x1b[?2026l');

Check failure on line 142 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use uppercase characters for the value of the escape sequence

Check failure on line 142 in packages/jest-reporters/src/DefaultReporter.ts

View workflow job for this annotation

GitHub Actions / Lint

Use Unicode escapes instead of hexadecimal escapes
}
}
}

protected __printStatus(): void {
const {content, clear} = this._status.get();
this._clear = clear;
Expand Down

0 comments on commit f3ae7aa

Please sign in to comment.