如何为Inquirer.js编写单元测试?

Osc*_*car 5 stdin unit-testing command-line-interface node.js inquirerjs

我想知道如何为npm包Inquirer.js编写单元测试,这是使CLI包更容易使用的工具。我已经阅读了这篇文章,但无法使其正常工作。

这是我的代码,需要进行测试:

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);
                });
            } );
Run Code Online (Sandbox Code Playgroud)

...这是用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);
                });
            });
        });
Run Code Online (Sandbox Code Playgroud)

我想,以填补标准输入process.env.TEST_LOGIN(来回答第一个问题Inquirer.js)和process.env.TEST_PASS_OR_AUTH(回答第二个问题Inquirer.js),看看是否该函数创建一个有效的配置文件(与值unix_login通过该方法猜到create的工厂对象)。

我试图了解Inquirer.js单元如何进行自我测试,但是对NodeJS的理解还不够。您可以帮助我进行此单元测试吗?

lag*_*lex 5

您只需模拟或存根不想测试的任何功能。

  • module.js -您要测试的模块的简化示例

    const inquirer = require('inquirer')
    
    module.exports = (questions) => {
      return inquirer.prompt(questions).then(...)
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • module.test.js

    const inquirer = require('inquirer')
    const module = require('./module.js')
    
    describe('test user input' () => {
    
      // stub inquirer
      let backup;
      before(() => {
        backup = inquirer.prompt;
        inquirer.prompt = (questions) => Promise.resolve({email: 'test'})
      })
    
      it('should equal test', () => {
        module(...).then(answers => answers.email.should.equal('test'))
      })
    
      // restore
      after(() => {
        inquirer.prompt = backup
      })
    
    })
    
    Run Code Online (Sandbox Code Playgroud)

有一些库可以帮助进行模拟 /存根,例如sinon

inquirer.prompt在这种情况下,也更容易进行模拟,因为.prompt这只是主导出中的一个属性inquirer,它将在module.js和中引用相同的对象module.test.js。对于更复杂的情况,有些库可以提供帮助,例如proxyquire。或者,您可以通过创建模块的方式来帮助您轻松切换依赖项以进行测试。例如:

  • module.js -使其成为一个“工厂”函数,该函数将返回您的主函数,并自动(通过默认参数)或手动注入依赖项。

    module.exports = ({
      inquirer = require('inquirer'),
    } = {}) => (questions) => {
      return inquirer.prompt(questions).then(...)
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • module.test.js

    const module = require('./module.js')
    
    describe('test user input' () => {
    
      const inquirer = {prompt: () => Promise.resolve({email: 'test'})};
    
      it('should equal test', () => {
        module({inquirer})(...).then(answers => answers.email.should.equal('test'))
      })
    })
    
    Run Code Online (Sandbox Code Playgroud)