1

I have created an npm script to run a single jest test file. To run the file I would type:

> npm start test:1 **/unit-test-filename.test.js

I want to make that even easier and type something like:

> jt unit-test-filename

So I've tried creating a bash script that looks like this:

#!/bin/bash -x
npm run test:1 **/$1.test.js

Unfortunately when I try running it it puts a single quote around last parameter and it make it so that npm script can't find the file. I get an output like this:

╰─± jt unit-test-filename                                                                                
+ npm run test:1 '**/unit-test-filename.test.js'

> cms2@0.1.2 test:1 /Users/path/to/my/current/dir
> cross-env NODE_ENV=dev node ./etc/jest1 "**/unit-test-filename.test.js"

Error!! Test file: **/unit-test-filename.test.js doesn't exist.

I've tried escaping the asterisk and tried a number of other things. Is this even possible with bash scripting?

0
5

You probably don't have globstar set in a shell script.

You can enable it with shopt -s globstar.

#!/bin/bash
set -x
shopt -s globstar
npm run test:1 **/"$1".test.js
3
  • Thank you! It is working now and I realized it was zsh that was making it work in my npm script so I had to change it to #!/bin/zsh to get it to work
    – Dustin
    Mar 14 '18 at 17:03
  • I guess I don't need the shopt -s globstar command either since it doesn't work in zsh. I was told that this doesn't work for mac users since mac has bash 3 and bash 4 is required.
    – Dustin
    Mar 14 '18 at 17:32
  • Yes, it's bash specific. You can write */*/* but that will only work for a specific directory depth. For arbitrary depths, there is find. Mar 14 '18 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.