3

I made a React app using Create-React-App. I have a testing script in my React app's package.json like this:

"test": "node -r @babel/register -r @babel/polyfill **/*.test.js | tap-color", 

This catches and executes the files in src/ like src/App.test.js, but not header.test.js in src/layout/header/header.test.js. And what if I add even more layers of folders? Is there a regex that will catch all folders in src/ and their subfolders no matter how nested with a file ending of .test.js?

Edit: I found this question on StackOverflow. According to that answer you would have to write:

"test": "node -r @babel/register -r @babel/polyfill 'src/**/*.test.js' | tap-color", 

which unfortunately matches nothing for me. I'm on Mac.

1 Answer 1

2

The question you refer to is not especially useful for your case. My answer there works because Mocha has been designed to pass the patterns you give to it to the glob library for interpretation. So when you do mocha 'src/app/**/*.tests.js' the quotes prevent the shell from interpreting the pattern, Mocha gets src/app/**/*.tests.js as the first pattern given to it, which it gives to glob to get a list of files to actually run. Your case is different at least one crucial way: glob is not involved so there is nothing that can correctly interpret **.

Your first attempt is consistent with what happens when you are using a shell that does not understand **. It is interpreted exactly the same as *. So the shell interprets **/*.test.js as */*.test.js, expands this pattern and passes the result to node.

In your second attempt, you quote the pattern but that does not help you because node does not do pattern interpretation. It tries to load a file at path src/**/*.test.js, interpreted literally. This is not what you want.

I'm not sure what the compatibility implication with Windows are, but you could replace 'src/**/*.test.js' with $(find src -type f -name '*.test.js'). (See the man page for details.) At run-time, the shell will replace this with the result of the find command.

Or for greater simplicity, and less risk of platform issues creeping up, you could use babel-tap like this:

babel-tap 'src/**/*.test.js' | tap-color 

If you use babel-tap, there's actually no need for using find because internally babel-tap calls on facilities that use the glob library to interpret the file names passed to it.


I've focused on the file pattern issue but I'm not seeing how what you're trying to do would work, even without the pattern issue. Consider this command, and assume that the files exist:

node -r @babel/register -r @babel/polyfill src/a.test.js src/b.test.js 

This is not telling Node to run src/a.test.js and src/b.test.js. Rather, it tells node "run the script src/a.test.js and pass to it the parameter src/b.test.js". I've not used tap very much but I don't recall it working this way. Using babel-tap like I show above also avoids the problem here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your extensive answer. Using "test": "node -r @babel/register -r @babel/polyfill $(find src -type f -name '*.test.js') | tap-color", works in that it finds the first file matching the pattern. But as you assumed, only runs the tests in the first file and skips the rest. (I think) I unfortunately can't use babel-tap since I'm using RITEway and that library imports and uses tape, so I have no control over the test function. Do you have any other idea how I could run all tests? Is there a way I could loop the test command until all files have been found?
Not familiar with RITEway. What I'd try is node -r @babel/register -r @babel/polyfill ./node_modules/.bin/riteway 'src/**/*.test.js'. (I've typed this off the top of my head. Watch out for typos and make sure the path to riteway is correct.) The change is just to pass the riteway script as the name of the script to run and give to it the glob pattern as argument. From the README of RITEway it seems that it supports glob patterns natively.
Holy cow, that works. Thank you so much, Louis, you helped me a lot. Having to run dozens of test suites manually after a refactor is tedious. No more! 👌🏻

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.