Will be grateful if someone could clarify to me how test the async code from inquirer plugin for CLI app.
The module exports updateView
function, which calls async inquirer.prompt
inside.
const inquirer = require("inquirer");
const getAnswer = async (request) => {
const answer = await inquirer.prompt(request);
return answer;
}
Want to test with Jest that async code works, however all the Jest examples I have seen show ways to test async code only if I pass async function as a parameter.
So my function will have to be refactored to that:
getAnswers.js
const getAnswer = async (request, callback) => {
const answer = await callback(request);
return answer;
}
main.js
const inquirer = require("inquirer");
const getAnswers = require("./getAnswers");
const main = async () => {
const request = "abc";
const result = async getAnswers(request, inquirer.prompt);
...
}
And then test file will look like that:
test.js
const getAnswers = require("./getAnswers");
test("async code works", async () => {
//Arrange
const mock = async () => {
return "Correct Answer";
};
//Act
const result = async getAnswers("abc", mock);
//Assert
expect(result).toEqual("Correct Answer";);
});
Will be very grateful if someone could suggest if there is a way of testing async function without passing it as a callback?
And if the approach itself is correct.
Thank you very much,
Katya
Aucun commentaire:
Enregistrer un commentaire