admin管理员组文章数量:1296260
I have already seen the question and answer on How to write unit tests for Inquirer.js?
I want to test that my validation is correct. So for example, if I have:
const answer = await inquirer.prompt({
name: 'answer',
message: 'are you sure?'
type: 'input',
validate: async (input) => {
if (input !== 'y' || input !== 'n') {
return 'Incorrect asnwer';
}
return true;
}
});
I want to run a unit test that I can run and verify that if I provided blah
, the validation will validate correctly. How can I write a test for this?
I have already seen the question and answer on How to write unit tests for Inquirer.js?
I want to test that my validation is correct. So for example, if I have:
const answer = await inquirer.prompt({
name: 'answer',
message: 'are you sure?'
type: 'input',
validate: async (input) => {
if (input !== 'y' || input !== 'n') {
return 'Incorrect asnwer';
}
return true;
}
});
I want to run a unit test that I can run and verify that if I provided blah
, the validation will validate correctly. How can I write a test for this?
1 Answer
Reset to default 8I would move validation to separate function, and test it in isolation. Both test and code will look clearer:
const confirmAnswerValidator = async (input) => {
if (input !== 'y' || input !== 'n') {
return 'Incorrect asnwer';
}
return true;
};
const answer = await inquirer.prompt({
name: 'answer',
message: 'are you sure?'
type: 'input',
validate: confirmAnswerValidator
});
and then in test
describe('Confirm answer validator', () => {
it('Raises an error on "blah"', async () => {
const result = await confirmAnswerValidator('blah');
expect(result).toBe('Incorrect asnwer');
});
});
本文标签: javascriptHow to test inquirer validationStack Overflow
版权声明:本文标题:javascript - How to test inquirer validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741599961a2387639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论