Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix waitUntilExit() not resolved with sync errors #563

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion src/ink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type Options = {
debug: boolean;
exitOnCtrlC: boolean;
patchConsole: boolean;
waitUntilExit?: () => Promise<void>;
initWaitUntilExit: boolean;
};

export default class Ink {
Expand Down Expand Up @@ -116,6 +116,13 @@ export default class Ink {
options.stdout.off('resize', this.resized);
};
}

if (options.initWaitUntilExit) {
this.exitPromise = new Promise((resolve, reject) => {
this.resolveExitPromise = resolve;
this.rejectExitPromise = reject;
});
}
}

resized = () => {
Expand Down
8 changes: 8 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export type RenderOptions = {
* @default true
*/
patchConsole?: boolean;

/**
* Automatically initialize `waitUntilExit` method in order to catch synchronous errors.
*
* @default false
*/
initWaitUntilExit?: boolean;
};

export type Instance = {
Expand Down Expand Up @@ -80,6 +87,7 @@ const render: RenderFunction = (node, options): Instance => {
debug: false,
exitOnCtrlC: true,
patchConsole: true,
initWaitUntilExit: false,
...getOptions(options)
};

Expand Down
8 changes: 8 additions & 0 deletions test/exit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ test.serial('exit with thrown error', async t => {
t.true(output.includes('errored'));
fantua marked this conversation as resolved.
Show resolved Hide resolved
});

test.serial(
'exit with thrown error when initWaitUntilExit is true',
async t => {
const output = await run('exit-with-thrown-error-fix');
t.true(output.includes('waitUntilExit catch: errored'));
}
);

test.serial('don’t exit while raw mode is active', async t => {
await new Promise<void>((resolve, _reject) => {
const env: Record<string, string> = {
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/exit-with-thrown-error-fix.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import {render} from '../../src/index.js';

const Test = () => {
throw new Error('errored');
};

const app = render(<Test />, {initWaitUntilExit: true});

try {
await app.waitUntilExit();
} catch (error: unknown) {
console.log(`waitUntilExit catch: ${(error as Error).message}`);
}