How does this pattern work: 'test/e2e/**/*.spec.js'?
Asked Answered
G

3

5

I saw this pattern used in a configuration file for protractor.

specs: [
  'test/e2e/**/*.spec.js'
]

To mean it means "all files inside test/e2e". What kind of pattern is this? I think it's not regex because of those unescaped slashes. Especially, why is there ** in the middle, not just test/e2e/*.spec.js?

I tried using the search engine, but did not find anything useful probably because the asterisks don't work very well in search engines.

Gaullism answered 31/12, 2015 at 22:22 Comment(0)
C
7

What kind of pattern is this?

It is called "glob". The module glob is a popular implementation for Node, and appears to be the one used by Protractor.

Especially, why is there "**" in the middle, not just "test/e2e/*.spec.js"?

** means it can match sub-directories. It's like a wildcard for sub-directories.

For example, test/e2e/*.spec.js would match test/e2e/example.spec.js, but not test/e2e/subdir/example.spec.js. However test/e2e/**/*.spec.js matches both.

Coffeehouse answered 31/12, 2015 at 22:28 Comment(0)
C
5

It is called "glob" syntax. Glob is a tool which allows files to be specified using a series of wildcards.

  • *.js means "everything in a folder with a js extension.
  • ** means "descendant files/folders.
  • **/*.js means "descendant files with a js extension in descendant folders."
  • test/e2e/**/*.spec.js' means the above, starting in the test/e2e/ directory.

So, for example, given this file system:

test/e2e/foo/a.spec.js <-- matches
test/e2e/foo/butter.js <-- does not include "spec.js"
test/e2e/bar/something.spec.js <-- matches
test/other/something-different.spec.js <-- not in /test/e2e

The final pattern would match:

test/e2e/foo/a.spec.js
test/e2e/bar/something.spec.js
Crotch answered 31/12, 2015 at 22:28 Comment(0)
H
3

It's a globbing pattern. Most javascript things using globbing patterns seem to be based around the glob npm package. It's worth taking a look at the documentation as there are some handy hints in there for when you have more complex situations.

The path you are asking about will match any file ending .spec.js in any subdirectory under test/e2e

Handler answered 31/12, 2015 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.