cli.js
const inquirer = require('inquirer');
const validateNameResponse = response => response.length <= 10;
const handleAnswers = answers = {
// whatever you want to do with answers
};
const question = {
type: 'input',
name: 'name',
message: 'Enter your name : ',
validate: validateNameResponse
};
inquirer
.prompt([question])
.then (handleAnswers);
module.exports = {
validateNameResponse,
};
Cli.unit.test.js(Cli.unit.test 是同一目录下的同级文件)
const {
validateNameResponse,
} = require('./cli');
describe('cli', () => {
for(let i =0; i <= 10; i++;) {
it(`should have function validateNameResponse pass inputted name that is ${i} characters long`, () => {
const name='a'.repeat(i);
expect(validateNameResponse(name)).toBe(true)
});
}
it('should have function validateNameResponse fail inputted name that is more than 10 characters', () => {
const name = 'a'.repeat(11);
expect(validateNameResponse(name)).toBe(false);
});
});