如何为Inquirer.js编写单元测试? [英] How to write unit tests for Inquirer.js?

查看:140
本文介绍了如何为Inquirer.js编写单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
                });
            } );

...这是用Mocha编写的测试:

...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);
                });
            });
        });

我想用 process.env.TEST_LOGIN <填充标准输入/ code>(回答第一个Inquirer.js问题)和 process.env.TEST_PASS_OR_AUTH (回答第二个Inquirer.js问题)以查看该函数创建一个有效的配置文件(其值unix_login由工厂对象的方法 create 猜测)。

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).

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

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?

推荐答案

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

You simply mock or stub any functionality that you don't want to test.


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

  • module.js - simplified example of a module you want to test

const inquirer = require('inquirer')

module.exports = (questions) => {
  return inquirer.prompt(questions).then(...)
}


  • module.test.js

  • 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
      })
    
    })
    


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

    There are libraries to help with mocking/stubbing, like sinon.

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

    Also it was easier to mock inquirer.prompt in this case because .prompt was just a property on the main export inquirer which will refer to the the same object in both module.js and module.test.js. For more complicated scenarios there are libraries that can help, like proxyquire. Or you can create your modules in a way that help you switch out the dependencies easily for testing. For example:


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

    • module.js - make it a "factory" function which returns your main function with dependencies injected either automatically (via default arguments) or manually.

    module.exports = ({
      inquirer = require('inquirer'),
    } = {}) => (questions) => {
      return inquirer.prompt(questions).then(...)
    }
    


  • module.test.js

  • 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'))
      })
    })
    


  • 这篇关于如何为Inquirer.js编写单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆