2

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 = await 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 = await 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.

1 Answers1

3

You can use jest.mock to mock the imported dependencies rather than pass them as parameters. Here is the unit test solution:

getAnswers.js:

const inquirer = require('inquirer');

const getAnswers = async (request) => {
  const answer = await inquirer.prompt(request);
  return answer;
};

module.exports = getAnswers;

getAnswers.test.js:

const getAnswers = require('./getAnswers');
const inquirer = require('inquirer');

jest.mock('inquirer', () => {
  return { prompt: jest.fn() };
});

describe('59495121', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });
  it('should pass', async () => {
    inquirer.prompt.mockResolvedValueOnce('Correct Answer');
    const actual = await getAnswers('abc');
    expect(actual).toBe('Correct Answer');
  });
});

Unit test result with 100% coverage:

 PASS  src/stackoverflow/59495121/getAnswers.test.js (10.172s)
  59495121
    ✓ should pass (6ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 getAnswers.js |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        11.367s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59495121

Lin Du
  • 88,126
  • 95
  • 281
  • 483