简体   繁体   English

使用Mocha进行测试也会启动主程序

[英]Running a test with Mocha also launches the main program

I'm trying to use Mocha to test a CLI app. 我正在尝试使用Mocha测试CLI应用程序。 The tests are running fine but, when I launch the testing procedure, it also launches the main app: 测试运行正常,但是当我启动测试过程时,它还会启动主应用程序:

$ npm run test

> standardize-js@0.2.2 test C:\Users\Gaspard\Documents\Code\standardize-js
> mocha "./source/**/*.spec.js"

? Choose your project language or framework (Use arrow keys) //<-- THIS IS THE PROGRAM
> Javascript 
  Typescript 
  AngularJS 

  Main function //<-- THIS IS THE TEST
    ask if the configuration is valid
Configuration is not valid, terminating program.
      √ should return false if the configuration is not accepted


  1 passing (29ms)

I'm kind of new to the testing world and I'm really struggling to understand what I'm doing wrong. 我是测试领域的新手,我真的很难理解自己在做错什么。
Here is the NPM script used to launch mocha : 这是用于启动mocha的NPM脚本:

"test": "mocha \"./source/**/*.spec.js\""

Here is my testing method: 这是我的测试方法:

/* eslint-disable func-names */
const { expect } = require("chai");

const main = require("./index").test;

describe("Main function", function() {
  describe("ask if the configuration is valid", function() {
    it("should return false if the configuration is not accepted", function() {
      const fakeAnswer = false;

      expect(main.validateConfiguration(fakeAnswer)).to.equal(false);
    });
  });
});

And here is my index.js file: 这是我的index.js文件:

function validateConfiguration(answer) {
  if (answer === false) {
    console.log(chalk.red("Configuration is not valid, terminating program."));
    return false;
  }
  return true;
}

const run = async () => {
//MAIN FUNCTION
};

run();

// Export functions and variables to be able to test
exports.test = {
  validateConfiguration
};

It's not a problem with mocha. 摩卡咖啡不是问题。 It is simply now node.js modules work. 现在仅是node.js模块即可工作。

When you do this: 执行此操作时:

const main = require("./index").test;

Node.js will execute index.js and then check the value of module.exports . Node.js将执行index.js ,然后检查module.exports的值。 If the module ( index.js ) sets or modifies module.exports then node will export it for use by require() . 如果模块( index.js )设置或修改了module.exports则node将导出它以供require() But note, in order for node to know that the module has exported anything it must execute the javascript file. 但是请注意,为了使节点知道模块已导出任何内容,它必须执行javascript文件。

Node.js does not have any ability to parse and analyze javascript syntax (that's V8's job). Node.js没有任何能力来解析和分析JavaScript语法(这是V8的工作)。 Unlike other languages such as C or Java, modules in node.js are not implemented at the syntax level. 与其他语言(例如C或Java)不同,node.js中的模块不是在语法级别上实现的。 Therefore the javascript language does not need to be modified (eg. ES6 modules) for node.js to support modules. 因此,不需要修改JavaScript语言(例如ES6模块)以使node.js支持模块。 Modules are simply implemented as a design pattern. 模块被简单地实现为设计模式。

In your index.js file you call run: 在index.js文件中,您调用run:

run();

When require() loads index.js it will therefore also cause run() to be called. require()加载index.js ,也会导致run()被调用。


Test libraries, not main 测试库,不是主要的

The solution to this is to implement your own logic as modules and test that, not test index.js : 解决方案是将自己的逻辑实现为模块并进行测试,而不是测试index.js

mylib.js : mylib.js

function validateConfiguration(answer) {
  if (answer === false) {
    console.log(chalk.red("Configuration is not valid, terminating program."));
    return false;
  }
  return true;
}

// Export functions and variables to be able to test
exports.test = { validateConfiguration };

index.js : index.js

const validateConfiguration = require("./mylib").test;

const run = async () => {
    //MAIN FUNCTION
};

run();

You can now use your test script as written. 现在,您可以按照编写的方式使用测试脚本。

How can you not test code?? 你怎么不测试代码?

The strategy to keep index.js bug free without testing is to remove all logic from it except for the minimum amount of code to wire all your other code up together to run the app. 未经测试就保持index.js错误的策略是从其中删除所有逻辑,除了将所有其他代码组合在一起以运行该应用程序的最少代码量。 The code should be as simple as "Hello World". 该代码应与“ Hello World”一样简单。 That way, the code in main is so small and so simple that you can test it for bugs using your eyeballs. 这样,main中的代码是如此之小,如此简单,以至于您可以使用眼球来测试它们的错误。

Any code in index.js that causes a bug should be refactored into its own library so that it can be tested separately. index.js中导致错误的任何代码都应重构到其自己的库中,以便可以对其进行单独测试。 There are a small handful of corner cases, such as loading environment variables or opening port 80 where you can't really separate into a library because they literally are wiring logic. 在极少数情况下,例如加载环境变量或打开端口80时,您实际上不能分离到库中,因为它们实际上是接线逻辑。 For such cases you just have to be really careful. 对于这种情况,您只需要非常小心。

之所以称其为run是因为您在定义方法后立即告诉它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM