简体   繁体   中英

Makefile command to run react test suite in child folder

I'm trying to create a Makefile that runs the test suite for my react app. From the root folder the react app is located at frontend/ .

What make command can I create the achieve this? I've tried the following but no luck:

test-frontend:
    cd ./frontend/
    npm test

test-frontend:
    npm test ./frontend/

test-frontend:
    cd ./frontend/
    npm run-script test

Here's the error I get:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /path/to/dir/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/path/to/dir/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /$HOME/.npm/_logs/2019-10-24T02_23_23_195Z-debug.log
make: *** [test-frontend] Error 254

The comments answered my question. The answer is:

test-frontend:
    cd ./frontend/ && npm run-script test

Or if you want to use separate lines for readability:

test-frontend:
    cd ./frontend/ && \
    npm run-script test

You are trying to do things for which a Makefile is not designed for. You already got bitten by one of Makefile caveats: each command line is run on it's own shell.

The rule of thumb is: a recipe of a rule should create a filename named like the target of the rule.

Here is a rule (to clarify the terms):

target:
  recipe command lines
  should create file named target

There are some exceptions to this rule of thumb. Most notably make clean and make install . Both typically do not create files named clean or install . One can argue that make test can also be an exception to this rule of thumb.

You only stumbled upon the change directory caveat. Which is a common caveat to stumble when writing makefiles. It is even mentioned in the manual: https://www.gnu.org/software/make/manual/html_node/Execution.html . But keep in mind this is only one of many caveats that make has.

If this is the extend of your (ab)use of makefile to run commands then all maybe well. But if you want to expand the functionality and you start to stumble from caveat to caveat then it is time to rethink your approach. Either extract the recipe to its own script or write a wrapper script around your makefile. And if you are thinking about adding argument handling to your makefile then read this first: Passing arguments to "make run"

Ironically you also use the command execution feature of npm: npm run-script . That certainly has its own set of caveats. I do not know because I have not used npm that much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM