avatar

What is the difference between src/**/!(__tests__)/ and src/!(__tests__)/**/ in glob

src/**/!(__tests__)/

  • This pattern means: "Start from src/, go through any number of subdirectories (*), then match any directory that is NOT __tests__"
  • It will exclude __tests__ directories at ANY level of the directory tree
  • Examples of what it matches:
    src/foo/                     ✓
    src/foo/bar/                 ✓
    src/foo/__tests__/           ✗
    src/foo/bar/__tests__/       ✗
    src/foo/bar/baz/             ✓

src/!(__tests__)/**/

  • This pattern means: "Start from src/, then match any directory that is NOT __tests__ at the FIRST level only, then match all subdirectories below that"
  • It only excludes __tests__ directories in the immediate children of src/
  • Examples of what it matches:
    src/foo/                     ✓
    src/foo/bar/                 ✓
    src/__tests__/               ✗
    src/foo/__tests__/           ✓ (Notice this IS matched!)
    src/foo/bar/baz/             ✓

The key difference is WHERE the exclusion happens in the directory tree. The first pattern excludes __tests__ everywhere, while the second pattern only excludes it at the first level under src/.