Skip to content

Commit

Permalink
Add proper error handling to legacy prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Dec 11, 2023
1 parent 34298d0 commit ea7c284
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
2 changes: 0 additions & 2 deletions packages/inquirer-autocomplete-prompt/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ function searchStates(answers, input = '') {
}

function searchFood(answers, input = '') {
// return Promise.reject(new Error('Something went wrong!'));
return new Promise((resolve) => {
setTimeout(() => {
resolve(fuzzy.filter(input, foods).map((el) => el.original));
Expand All @@ -110,7 +109,6 @@ inquirer
source: searchFood,
pageSize: 4,
validate(val) {
throw new Error('val error');
return val ? true : 'Type something!';
},
},
Expand Down
12 changes: 2 additions & 10 deletions packages/inquirer-autocomplete-prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AutocompletePrompt extends Base {
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
_run(cb /*: Function */, errCb) /*: this*/ {
_run(cb /*: Function */, errCb /*: Function */) /*: this*/ {
this.done = cb;
this.errCb = errCb;

Expand Down Expand Up @@ -179,11 +179,7 @@ class AutocompletePrompt extends Base {
);
}

if (isPromise(validationResult)) {
validationResult.then(checkValidationResult, checkValidationResult);
} else {
checkValidationResult(validationResult);
}
validationResult.then(checkValidationResult, this.errCb);
} else {
this.onSubmitAfterValidation(lineOrRl);
}
Expand Down Expand Up @@ -364,8 +360,4 @@ function listRender(choices, pointer /*: string */) /*: string */ {
return output.replace(/\n$/, '');
}

function isPromise(value) {
return typeof value === 'object' && typeof value.then === 'function';
}

export default AutocompletePrompt;
2 changes: 1 addition & 1 deletion packages/inquirer-autocomplete-prompt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"flow": "flow",
"pretest": "npm run lint && npm run flow",
"test": "vitest run test --coverage",
"develop": "vitest watch test --coverage --reporter=basic"
"develop": "vitest watch test -c ../../vitest.develop.config.mjs"
},
"type": "module"
}
48 changes: 37 additions & 11 deletions packages/inquirer-autocomplete-prompt/test/spec/indexSpec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ describe('inquirer-autocomplete-prompt', () => {

const validate = sinon.stub();

let fail = true;
prompt = new Prompt(
{
message: 'test',
Expand All @@ -433,22 +432,49 @@ describe('inquirer-autocomplete-prompt', () => {
);

promiseForAnswer = getPromiseForAnswer();
validate.throws(error);
enter();

validate.throws(error);
sinon.assert.calledOnce(validate);
const successSpy = sinon.spy();
const errorSpy = sinon.spy();

await promiseForAnswer.then(successSpy).catch(errorSpy);

sinon.assert.notCalled(successSpy);
sinon.assert.calledOnce(errorSpy);
});

it('renders async error in validate function', async () => {
const error = new Error('Something went wrong in validation!');
source.returns(['foo', 'bar']);

const validate = sinon.stub();

prompt = new Prompt(
{
message: 'test',
name: 'name',
validate,
source,
},
rl
);

validate.returns(true);
promiseForAnswer = getPromiseForAnswer();
validate.rejects(error);
enter();
sinon.assert.calledTwice(validate);

return promiseForAnswer.then((answer) => {
assert.equal(answer, 'foo');
});
const successSpy = sinon.spy();
const errorSpy = sinon.spy();

await promiseForAnswer.then(successSpy).catch(errorSpy);

sinon.assert.notCalled(successSpy);
sinon.assert.calledOnce(errorSpy);
});
});

describe('default behaviour', () => {
describe('default behavior', () => {
it('sets the first to selected when no default', () => {
prompt = new Prompt(
{
Expand Down Expand Up @@ -568,7 +594,7 @@ describe('inquirer-autocomplete-prompt', () => {
});
});

it('applies filter async with done calback', () => {
it('applies filter async with done callback', () => {
prompt = new Prompt(
{
message: 'test',
Expand Down Expand Up @@ -867,7 +893,7 @@ describe('inquirer-autocomplete-prompt', () => {
sinon.assert.callCount(source, 4);
});

it('does not search again if same searchterm (not input added)', () => {
it('does not search again if same search term (not input added)', () => {
type('ice');
sinon.assert.calledThrice(source);
source.reset();
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer-autocomplete-standalone/demo/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const states = [
];

function searchStates(input = ''): Promise<ChoiceOrSeparatorArray<string>> {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
setTimeout(() => {
const results = states.filter((s) =>
s.toLowerCase().includes(input.toLowerCase())
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer-autocomplete-standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
"clean": "rm -rf dist",
"build": "yarn run clean && yarn run tsc",
"tsc": "tsc -p ./tsconfig.json",
"develop": "vitest watch test --coverage --reporter=basic"
"develop": "vitest watch test -c ../../vitest.develop.config.mjs"
}
}
10 changes: 10 additions & 0 deletions vitest.develop.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
reporter: ['basic'],
coverage: {
reporter: ['lcov'],
},
},
});

0 comments on commit ea7c284

Please sign in to comment.