I was wondering how to write unit tests for the npm package Inquirer.js, which is a tool to make CLI package more easily. I have read this post but I was't able to make it works.
Here is my code that needs to be tested:
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);
});
} );
...and here is the test, written with 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);
});
});
});
I'd like to fill stdin with the process.env.TEST_LOGIN
(to answer the first Inquirer.js question) and process.env.TEST_PASS_OR_AUTH
(to answer the second Inquirer.js question) to see if the function create a valid profile (with the value unix_login guessed by the method create
of the factory object).
I tried to understand how Inquirer.js unit tests itself, but my understanding of NodeJS isn't good enough. Can you help me with this unit test?
echo "test" | node index.js
. The "index.js" file corresponds to this snippet : nodejs.org/dist/latest-v8.x/docs/api/… It produce the following expected output :OHAI> test Say what? I might have heard 'test' OHAI> Have a great day!
– Mender