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

编程入门 行业动态 更新时间:2024-10-24 02:31:11
本文介绍了如何为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 (回答第一个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编写单元测试?

    本文发布于:2023-11-15 13:48:52,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1594996.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:何为   单元测试   Inquirer   js

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!