Running Istanbul test coverage for Node modules via shell script wrappers

302 views Asked by At

The scripts in my package.json currently looks like this:

"scripts": {
  "test": "./spec/run-local-tests.sh",
  "coverage": "istanbul cover jasmine-node spec",
  "start": "gulp"
}

The test script runs this .sh file:

#!/bin/sh

echo "Renaming database file produced by previous test run"
mv -f 'shared-local-instance.db' 'shared-local-instance.db.previous'
echo "Starting DynamoDB"
java -Djava.library.path=./DynamoDBLocal_lib -jar dynamodb/DynamoDBLocal.jar -sharedDb &
export JAVA_PID=$!
echo "Running Tests"
./node_modules/jasmine-node/bin/jasmine-node spec
echo "Cleaning up DynamoDB - killing local instance"
kill -9 $JAVA_PID

However I now want to change my coverage script to run the shell file:

"coverage": "istanbul cover ./spec/run-local-tests.sh"

But I then get an error that says:

echo "Renaming database file produced by previous test run"
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string

Is there a way that I can get instanbul and my shell file to run?

1

There are 1 answers

1
slim On BEST ANSWER

RTFM.

The Istanbul docs say:

The cover command can be used to get a coverage object and reports for any arbitrary node script.

(emphasis mine)

istanbul cover needs a node script. It can't do coverage on shell scripts or any other arbitrary executable.

You could write a shell script that does whatever setup/cleanup you need, then launches istanbul cover, and configure it thus:

"scripts": {
  "test": "./spec/run-local-tests.sh",
  "coverage": "./spec/run-local-tests-with-coverage.sh",
  "start": "gulp"
}

Or you could modify your existing script, perhaps like this:

echo "Running Tests"
${COVERAGE} ./node_modules/jasmine-node/bin/jasmine-node spec

Then call it with the environment variable set:

"scripts": {
  "test": "./spec/run-local-tests.sh",
  "coverage": "env COVERAGE='istanbul coverage' ./spec/run-local-tests.sh",
  "start": "gulp"
}