kentcdodds kentcdodds - 4 months ago 6
Javascript Question

What's wrong with my glob?

I'm trying to write a glob for something that uses

and having trouble excluding the files I want to. Here's a test case:

const glob = 'src/js/*!(test|stub).js' // this is the glob I'm trying to work out

const shouldMatch = [

const shouldNotMatch = [

const all = shouldMatch.concat(shouldNotMatch)

const matched = minimatch.match(all, glob) //

expect(matched).toEqual(shouldMatch) //

The output with this is:

"Error: Expected [ 'src/js/foo.js', 'src/js/bar.js', 'src/js/baz.js', 'src/js/foo.test.js', 'src/js/bar.stub.js' ] to equal [ 'src/js/foo.js', 'src/js/bar.js', 'src/js/baz.js' ]

You can see a live version of this here.

As you can see, it's including both the
and the
files. What does my glob need to be so those files are excluded?


This is a common mistake with the !(...) syntax. Imagine something like this:

ls *!(.png|.jpg)

This doesn't do what we want because the .png and .jpg are matched by the *. So once we get to !(.png|.jpg), the string is null.

To fix it, we can do:

ls !(*.png|*.jpg)

Thus, to fix your example:

const glob = 'src/js/!(*test|*stub).js'

Here's a working example