Search code examples
makefileistanbulnyc

How do I run nyc merge from Makefile?


I've inherited a JS code base with Jasmine unit tests. The testing framework uses karma and instanbul-combine to get code coverage. It seems istanbul-combine isn't working with present node modules, and besides is no longer maintained: the recommended replacement is nyc. I'm having trouble replacing istanbul-combine with nyc in the Makefile.

Here's are my attempts at merging the data (not even trying to get a report yet):

#1

@for dir in $(shell ls -d coverage/*/); do \
    echo "Merging $${dir}"; \
    npx nyc merge $${dir} coverage-final.json; \
done

#2

npx nyc merge coverage coverage-final.json

#3

npx nyc merge --include coverage/*/ coverage-final.json

The coverage data is in coverage/*/coverage-final.json, but none of these attempts succeeds in mergeing it into the result file coverage-final.json.

With #1, I'm pretty sure it's only actually merging a single set of results into the result file. With #2, there's an error; but if I put that command in the shell CLI, nothing is put into the result file.

With #3, at least there's no error, but only one of the coverage files is merged.


Here's the original Makefile line that I'm replacing:

PATH=$(PROJECT_HOME)/bin:$$PATH node_modules/istanbul-combine/cli.js \
    -d coverage/summary -r html \
    coverage/*/coverage-final.json

Solution

  • I wrote a little script in the Makefile to copy the coverage-final.json files from the child directories of the coverage directory to the coverage directory itself, and then merge them into a coverage-final.json file in the main JS directory.

    @cd coverage; \
     for dir in $(dir */coverage-final.json); do \
            fn="$${dir}coverage-final.json"; \
        newName="$${dir::-1}.json"; \
        echo "cp $${fn} $${newName}"; \
        cp $$fn $$newName; \
    done;
    npx nyc merge coverage coverage-final.json
    

    The new filenames of the individual coverage files are taken from the name of the directories from which they come.