Search code examples
bashcasperjstravis-ciexit-code

CasperJS pass exit code to Bash


I have a problem with running my CasperJS tests on Travis CI.

Whenever a test fails CasperJS returns status code 1, which would be the correct status code to be returned on a failed test.

I am running all my tests with a bash script and I would need the exit code of the tests in the bash script. I tried the $? operator, but this only returns wheter the command was executed properly or not. Since it is done properly it always returns 0.

So my question is: Is there a way to pass the CasperJs-Test status code to my bash script?

The reason I need all this is that I am running my tests on Travis CI and Travis always exits with status 0, since the tests are executed correctly and I would need to have Travis exit with the proper exit codes.

UPDATE:

Here is my script:

#!/bin/sh


WIDGET_NAME=${1:-widget} # defaults to 'widget'
PORT=${2:-4001} # default port is 4001
SERVER_PORT=${3:-4002} # default port is 4002
TEST_CASES=${4:-./test/features/*/*/*-test.casper.js} # default run     all subdirectories

# bail on errors
set -e

# switch to root folder
cd `dirname $0`/..

echo "Starting feature tests ..."

echo "- start App server on port $PORT"
WIDGET_NAME_PASCAL_CASE=`node -e "console.log(require('pascal-case')    (process.argv[1]))" $WIDGET_NAME`
./node_modules/.bin/beefy app/widget.js $PORT \
  --cwd public \
  --index public/widget-test.html \
  -- \
  --standalone $WIDGET_NAME_PASCAL_CASE \
    -t [ babelify --sourceMapRelative . ] \
    -t browserify-shim \
  --exclude moment 1>/dev/null &
echo $! > /tmp/appointment-widget-tester-process1.pid
sleep 1

echo "- start Fake API server on port $SERVER_PORT"
bin/fake-api $SERVER_PORT 1>/dev/null &
echo $! > /tmp/appointment-widget-tester-process2.pid
sleep 1

echo "- run feature tests"
mocha-casperjs $TEST_CASES --viewport-width=800 --viewport-height=600 --fail-fast | grep --line-buffered -v -e '^$' | grep --line-buffered -v "Unsafe JavaScript"

echo "- stop App and Fake API server"
kill -9 `cat /tmp/appointment-widget-tester-process*.pid`
rm /tmp/appointment-widget-tester-process*.pid

echo "done."

Solution

  • I have found my problem:

    It lies in the nature of the | operator! The first operation is the start of my tests and the second operation after the | operator is the grep and my $? references to the last command on the console, therefore it returns the exit code of the grep not the mocha-casperjs-runner

    A solution: Pipe output and capture exit status in Bash