我在想如何为npm包Inquirer.js编写单元测试,它是一个使CLI包更易于使用的工具。我已经阅读了这篇文章,但我无法让其运行。
我希望使用
我试图理解Inquirer.js如何对自身进行单元测试,但我对NodeJS的理解不够好。你能帮我完成这个单元测试吗?
以下是需要测试的代码:
const questions = [
{
type: 'input',
name: 'email',
message: "What's your email ?",
},
{
type: 'password',
name: 'password',
message: 'Enter your password (it will not be saved neither communicate for other purpose than archiving)'
}
];
inquirer.prompt(questions).then(answers => {
const user = create_user(answers.email, answers.password);
let guessing = guess_unix_login(user);
guessing.then(function (user) {
resolve(user);
}).catch(function (message) {
reject(message);
});
} );
...这是使用Mocha编写的测试:
describe('#create_from_stdin', function () {
this.timeout(10000);
check_env(['TEST_EXPECTED_UNIX_LOGIN']);
it('should find the unix_login user and create a complete profile from stdin, as a good cli program', function (done) {
const user_expected = {
"login": process.env.TEST_LOGIN,
"pass_or_auth": process.env.TEST_PASS_OR_AUTH,
"unix_login": process.env.TEST_EXPECTED_UNIX_LOGIN
};
let factory = new profiler();
let producing = factory.create();
producing.then(function (result) {
if (JSON.stringify(result) === JSON.stringify(user_expected))
done();
else
done("You have successfully create a user from stdin, but not the one expected by TEST_EXPECTED_UNIX_LOGIN");
}).catch(function (error) {
done(error);
});
});
});
我希望使用
process.env.TEST_LOGIN
(回答第一个Inquirer.js问题)和process.env.TEST_PASS_OR_AUTH
(回答第二个Inquirer.js问题)填充stdin,以查看函数是否创建了有效的配置文件(使用工厂对象的create
方法猜测unix_login的值)。我试图理解Inquirer.js如何对自身进行单元测试,但我对NodeJS的理解不够好。你能帮我完成这个单元测试吗?
echo "test" | node index.js
。"index.js" 文件对应于此代码片段:https://nodejs.org/dist/latest-v8.x/docs/api/readline.html#readline_readline它会产生以下预期输出:OHAI> test Say what? I might have heard 'test' OHAI> Have a great day!
- Oscar