0

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."
Fabio
  • 103
  • 10
  • Have a look at the answers in this question, they might help http://stackoverflow.com/questions/17176452/how-to-get-casper-js-to-return-an-exit-code-that-indicates-test-success-status – Alberto Zaccagni Nov 17 '15 at 15:20
  • See the thing is that my casper actually returns the right exit code. I tested it with `casper.on('exit',function(msg) {casper.echo(msg(})` and it returned 1 on failure, but my bash script still had 0 when I typed `echo $?` after the command – Fabio Nov 17 '15 at 15:22
  • Could you include your bash script (or an example bash script that illustrates the problem)? I suspect adding `set -e` to the top of your bash script might do the trick, but maybe there is a better solution – nfranklin Nov 17 '15 at 15:44
  • If `casper` exits with a non-zero exit status then `$?` will contain that status immediately after it returns. If you aren't seeing that then something else is likely running in between and clobbering the `casper` return status. Or you have some `casper` running wrapper that is clobbering it itself. In either case showing the script you are using is a good idea. – Etan Reisner Nov 17 '15 at 16:11
  • I have added the script! Please be so kind and offer your advice! – Fabio Nov 20 '15 at 08:19

1 Answers1

0

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

Community
  • 1
  • 1
Fabio
  • 103
  • 10