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

[Draft] Unit test demoing async failures not reported through the promise #1213

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions packages/core/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
hooks.length = 0;
sessionRl = rl;

// TODO: we should display a loader while we get the default options.

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 146 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'
const resolvedConfig = await getPromptConfig(config);

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -179,18 +179,22 @@
};

const workLoop = () => {
index = 0;
hooksEffect.length = 0;
handleChange = () => workLoop();
try {
index = 0;
hooksEffect.length = 0;
handleChange = () => workLoop();

const nextView = view(resolvedConfig, done);
for (const effect of hooksEffect) {
effect();
}
const nextView = view(resolvedConfig, done);
for (const effect of hooksEffect) {
effect();
}

const [content, bottomContent] =
typeof nextView === 'string' ? [nextView] : nextView;
screen.render(content, bottomContent);
const [content, bottomContent] =
typeof nextView === 'string' ? [nextView] : nextView;
screen.render(content, bottomContent);
} catch (err) {
reject(err);
}
};

workLoop();
Expand Down
14 changes: 14 additions & 0 deletions packages/input/input.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ describe('input prompt', () => {
await expect(answer).resolves.toEqual('2');
});

it('handle errors in validation gracefully', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer 2 ===',
validate() {
return Promise.reject(new Error('Validation error'));
},
});

events.type('1');
events.keypress('enter');
await expect(answer).rejects.toThrowError('Validation error');
expect(getScreen()).toMatchInlineSnapshot();
});

it('handle default option', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
Expand Down