运行 使用 Mocha 的测试也启动了主程序

Running a test with Mocha also launches the main program

我正在尝试使用 Mocha 来测试 CLI 应用程序。测试 运行 很好,但是当我启动测试程序时,它也会启动主应用程序:

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

我是测试界的新手,我真的很难理解我做错了什么。
这是用于启动 mocha 的 NPM 脚本:

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

这是我的测试方法:

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

这是我的 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
};

它正在调用 run 因为您在定义方法后立即告诉它。

摩卡不是问题。现在 node.js 模块可以正常工作。

当你这样做时:

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

Node.js会执行index.js然后检查module.exports的值。如果模块 (index.js) 设置或修改 module.exports,则节点将导出它以供 require() 使用。但是请注意,为了让节点知道模块已经导出任何东西,它 必须 执行 javascript 文件。

Node.js 没有任何解析和分析 javascript 语法的能力(这是 V8 的工作)。与 C 或 Java 等其他语言不同,node.js 中的模块未在语法级别实现。因此,javascript 语言无需修改(例如 ES6 模块)即可使 node.js 支持模块。模块只是作为一种设计模式实现的。

在您的 index.js 文件中调用 运行:

run();

require() 加载 index.js 时,它也会导致 run() 被调用。


测试库,不是主库

这个问题的解决方案是将您自己的逻辑实现为模块并对其进行测试,而不是测试 index.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:

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

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

run();

您现在可以使用编写的测试脚本。

怎么能不测试代码呢??

在不进行测试的情况下保持 index.js 无错误的策略是从中删除所有逻辑,除了将所有其他代码连接到 运行 应用程序的最少量代码之外。代码应该像 "Hello World" 一样简单。这样,main 中的代码就非常小而且非常简单,您可以用眼球测试它是否存在错误。

index.js 中任何导致错误的代码都应该重构到它自己的库中,以便可以单独测试。有少数极端情况,例如加载环境变量或打开端口 80,您不能真正分离到库中,因为它们实际上是接线逻辑。对于这种情况,你只需要非常小心。