简体   繁体   English

Mocha 未在子文件夹中提取测试

[英]Mocha Not Picking Up Tests in Sub-Folder

I am setting up some tests in my Node project using Mocha with Chai.我正在使用 Mocha 和 Chai 在我的 Node 项目中设置一些测试。 I have numerous tests running successfully, and now I'd like to group them a little more logically.我有许多成功运行的测试,现在我想对它们进行更合乎逻辑的分组。 Right now all tests are located in one "test" folder, which is at the root of the project.现在所有测试都位于一个“test”文件夹中,该文件夹位于项目的根目录中。

I'm noticing that when I create a sub folder within that "test" folder, and put a test in there, that it never gets run by Mocha.我注意到当我在该“测试”文件夹中创建一个子文件夹并在其中放置一个测试时,它永远不会被 Mocha 运行。 Any idea why this is happening?知道为什么会这样吗? My understanding is that Mocha runs tests within any folder named "test", as well as any sub-directories within that "test" folder.我的理解是 Mocha 在任何名为“test”的文件夹以及该“test”文件夹中的任何子目录运行测试。

My package.json look like this:我的 package.json 看起来像这样:

{
  "name": "event_runner",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "mocha || true",
    "start": "node server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "agenda": "^2.1.0",
    "agendash": "^1.0.0",
    "express": "^4.17.1",
    "mariadb": "^2.1.1",
    "mongoose": "^5.7.3"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "chai-datetime": "^1.5.0",
    "mocha": "^6.2.1"
  }
}

To get tests to run I was just typing mocha from the command line.为了让测试运行,我只是从命令行输入mocha This works ONLY when all tests are in one root folder.这仅在所有测试都在一个根文件夹中时才有效。 I did find a solution to run all tests, including those in sub-folders.我确实找到了运行所有测试的解决方案,包括子文件夹中的测试。 On my mac I have to run this from the command line:在我的 Mac 上,我必须从命令行运行它:

mocha "./test/" --recursive

BONUS: To exit after running tests, add the --exit flag:奖励:要在运行测试后退出,请添加--exit标志:

mocha "./test/" --recursive --exit

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

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