简体   繁体   English

如何配置 mocha 以递归方式查找所有测试文件?

[英]How to configure mocha to find all test files recursively?

How can I configure mocha to find all test files in my project directory and not only the test files at the test folder in the root directory?如何配置 mocha 以查找项目目录中的所有测试文件,而不仅仅是根目录中测试文件夹中的test文件?

currently, I'm using npm scripts in package.json:目前,我在 package.json 中使用 npm 脚本:

"test": "mocha"

and running using the following command:并使用以下命令运行:

npm run test

To find all test files in your src directory, say, then you should be able to use a pattern like this:例如,要在src目录中查找所有测试文件,您应该能够使用这样的模式:

mocha "src/**/*.test.js"

...and make sure your test files have a .test.js file extension so they differ from your other .js files. ...并确保您的测试文件具有.test.js文件扩展名,以便它们与您的其他.js文件不同。

(Note the double quotes around the pattern) (注意模式周围的双引号)

this works for me, finding all test files in all directories except the node_modules这对我有用,在除 node_modules 之外的所有目录中查找所有测试文件

mocha \"./{,!(node_modules)/**/}*.test.js\"

for clarification, this glop expression says:为了澄清起见,这个 glop 表达式说:

Take all files which end with .test.js in the current directory - which is the root - or in any subdirectory of the current directory, regardless of depth, except for ones that exists in ./node_modules folder.获取当前目录中以 .test.js 结尾的所有文件 - 这是根目录 - 或当前目录的任何子目录中,无论深度如何,除了 ./node_modules 文件夹中存在的文件。

For me "test": "./src/**/*.test.js" was not working, I've to add escaped quotes in the directory pattern对我来说 "test": "./src/**/*.test.js" 不起作用,我必须在目录模式中添加转义引号

{
 "scripts": {
    "test": "mocha \"./src/tests/**/*.test.js\""
  }
}

I was testing this out and wanted to try to run only one of the test files(not it tests, but actual separate files) in the terminal using mocha, but can't seem to come around it.我正在对此进行测试,并想尝试使用 mocha 在终端中仅运行一个测试文件(不是它测试,而是实际的单独文件),但似乎无法绕过它。

I attached a screenshot below for explanation.我附上了下面的屏幕截图以供解释。

What I want to do is have only my test for killer robot run and not the app test.我想做的是只运行我对杀手机器人的测试,而不是应用程序测试。

VS code image VS 代码图片

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

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