本文介绍了mocha如何递归搜索我的`src`文件夹以获取特定的文件名模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的npm包中,我想模仿Meteor遵循的模式:源文件(名为 client.js )有一个测试文件(名为<$ c $) c> client.tests.js )位于 src / 文件夹中。测试使用 npm test 命令运行。

In my npm package, I would like to emulate the pattern Meteor follows: a source file (named client.js) has a test file (named client.tests.js) live in a src/ folder. Tests run with the npm test command.

我正在使用't'的使用文档。我不想在我的包测试命令中使用 find

I'm following the usage docs to the 't'. I do not want to use a find in my package test command.


  1. 我明白mocha可以递归执行测试:

  1. I understand that mocha can recursively execute tests:

mocha --recursive

mocha --recursive

我明白了mocha可以使用 - 递归标志在特定子文件夹中执行测试:

I understand that mocha can execute tests in a specific subfolder using the --recursive flag:

mocha src --recursive

mocha src --recursive

我也明白我可以指定一个通过传递 * .test.js 来过滤文件的glob:

I also understand that I can specify a glob to filter files by passing *.tests.js:

mocha * .tests.js

mocha *.tests.js

但是,我想要这三个。我希望mocha只测试src文件夹中以 tests.js 结尾的文件,递归检查子目录。

But, I want all three. I want mocha to test only files ending in tests.js in the src folder, recursively checking subdirectories.

mocha --recursive *.tests.js

// See the files?
$ > ll ./src/app/
total 168
-rw-r--r--  ... client.js
-rw-r--r--  ... client.tests.js

// Option A
$ > mocha --recursive *.tests.js
Warning: Could not find any test files matching pattern: *.tests.js
No test files found

// Option B
$ > mocha *.tests.js --recursive
Warning: Could not find any test files matching pattern: *.tests.js
No test files found.

// Option C
$ > mocha --recursive src/app/*.tests.js
3 passing (130ms)
3 failing

所以......


  1. 为什么mocha没有拿起 *。测试。子文件夹中的js 文件?

  2. 如果我指定文件的完整路径,为什么它会起作用?

  3. 如何使其按预期工作?

  1. Why is mocha not picking up the *.tests.js files in the subfolders?
  2. Why DOES it work if I specify the full path to the file?
  3. How do I make it work as desired?


推荐答案

- 递归标志是为了操作在目录上。如果你要传递一个匹配目录的glob,那么这些目录将被递归检查,但如果你传递一个匹配文件的glob,就像你正在做的那样,那么 - recursive 是无效的。我建议不要使用带有glob的 - recursive ,因为globs已经具有在子目录中递归查看的能力。你可以这样做:

The --recursive flag is meant to operate on directories. If you were to pass a glob that matches directories, then these directories would be examined recursively but if you pass a glob that matches files, like you are doing, then --recursive is ineffective. I would suggest not using --recursive with a glob because globs already have the capability to look recursively in subdirectories. You could do:

mocha 'src/app/**/*.tests.js'

这将匹配所有与 *。tests.js 匹配的文件的src /应用。请注意我是如何在模式周围使用单引号的。这是引用模式,以便它按原样传递给Mocha的globbing代码。否则,你的shell可能会解释它。根据选项,某些shell会将 ** 转换为 * ,您将无法获得所需的结果。

This would match all files that match *.tests.js recursively in src/app. Note how I'm using single quotes around the pattern. This is to quote the pattern so that it is passed as-is to Mocha's globbing code. Otherwise, your shell might interpret it. Some shells, depending on options, will translate ** into * and you won't get the results you want.

这篇关于mocha如何递归搜索我的`src`文件夹以获取特定的文件名模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:50