Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic error case test support #860

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ lib-cov:

test: test-unit

test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-glob
test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers \
test-glob test-error

test-jsapi:
@node test/jsapi
Expand Down Expand Up @@ -104,6 +105,9 @@ test-async-only:
test-glob:
@./test/acceptance/glob/glob.sh

test-error:
@./test/acceptance/error/error.sh

non-tty:
@./bin/mocha \
--reporter dot \
Expand Down
15 changes: 15 additions & 0 deletions test/acceptance/error/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

// Known issue: #270
// This will currently fail and stop further execution of mocha entirely.
// describe('failure during beforeEach', function(){
// beforeEach(function(){
// throw new Error("Failure during beforeEach");
// })
//
// it('should fail during beforeEach #1', function(){
// })
//
// it('should fail during beforeEach #2', function(){
// })
// })

94 changes: 94 additions & 0 deletions test/acceptance/error/error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash
REL_SCRIPT_DIR="`dirname \"$0\"`"
SCRIPT_DIR="`( cd \"$REL_SCRIPT_DIR\" && pwd )`"
CALL_MOCHA=$SCRIPT_DIR/../../../bin/mocha
ERRORCASE_ROOT=$SCRIPT_DIR

ERRORCASE_FILES=./*.js
ERRORCASE_OUTPUT=/tmp/mocha-errorcase.txt
ERRORCASE_RESULT_PATTERNFILE=$SCRIPT_DIR/match-pattern.txt
ERRORCASE_EXCLUDE_PATTERNFILE=$SCRIPT_DIR/exclude-pattern.txt

cd $ERRORCASE_ROOT || {
echo Could not cd to $SCRIPT_DIR from `pwd`
exit 1
}

$CALL_MOCHA -R json-stream $ERRORCASE_FILES > $ERRORCASE_OUTPUT && {
echo "$ERRORCASE_FILES" in `pwd` should return failing exit code.
exit 1
}


# All lines must match one of the following patterns.
# start line
# Cases which should fail should include "should fail" in their description.
# Cases which should pass should include "should pass" in their description.
# end line with count
# NOTE: Update the numbers in end line as needed to match expected results.
# This is a bit crude but sufficient for now.
# This also makes the pattern file a need to change with every added test. Bummer.

# This process is a little brittle - regex being what it is.
# Sanity check the pattern file against output that should never report a match.
echo '["end",{"suites":0,"tests":0,"passes":0,"pending":0,"failures":0,"start":"1999-12-31T23:59:58.000Z","end":"1999-12-31T23:59:59.000Z","duration":0}]' | egrep -f $ERRORCASE_RESULT_PATTERNFILE && {
echo
echo ======================================================================
echo Sanity check on $ERRORCASE_RESULT_PATTERNFILE reported match where none was expected.
echo Either your environment is incorrectly configured or the last edit to the pattern file was bad.
echo ======================================================================
exit 1
}

# check for excluded patterns
cat $ERRORCASE_OUTPUT | egrep -f $ERRORCASE_EXCLUDE_PATTERNFILE && {
echo
echo ======================================================================
echo Results for "$ERRORCASE_FILES" in `pwd` do not match expected.
echo ======================================================================
echo
echo ======================================================================
echo The above lines match one of the excluded patterns in "$ERRORCASE_EXCLUDE_PATTERNFILE":
echo
cat $ERRORCASE_EXCLUDE_PATTERNFILE
echo
echo
echo One of the following has happened:
echo Test marked "should not run" reported a result
echo
echo Full json output may be found at "$ERRORCASE_OUTPUT".
echo
echo To see general test output run the following from `pwd`:
echo $CALL_MOCHA "$ERRORCASE_FILES"
echo ======================================================================
exit 1
}

# check for required patterns
cat $ERRORCASE_OUTPUT | egrep -v -f $ERRORCASE_RESULT_PATTERNFILE && {
echo
echo ======================================================================
echo Results for "$ERRORCASE_FILES" in `pwd` do not match expected.
echo ======================================================================
echo
echo ======================================================================
echo The above lines do not match any of the patterns in "$ERRORCASE_RESULT_PATTERNFILE":
echo
cat $ERRORCASE_RESULT_PATTERNFILE
echo
echo
echo One of the following has happened:
echo Run did not start
echo Test marked "should pass" reported fail
echo Test marked "should fail" reported pass
echo Tests were added, changed, or removed - the counts in the "end" line need to change
echo
echo Full output may be found at "$ERRORCASE_OUTPUT".
echo
echo To see general test output run the following from `pwd`:
echo $CALL_MOCHA "$ERRORCASE_FILES"
echo ======================================================================
exit 1
}

echo Error case results matched expected.
1 change: 1 addition & 0 deletions test/acceptance/error/exclude-pattern.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\[".*",[{]"title":".*should not run.*",
4 changes: 4 additions & 0 deletions test/acceptance/error/match-pattern.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\["start"
\["fail",[{]"title":".*should fail.*",
\["pass",[{]"title":".*should pass.*",
\["end",[{]"suites":4,"tests":8,"passes":4,"pending":0,"failures":4,
61 changes: 61 additions & 0 deletions test/acceptance/error/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

describe('timeouts', function(){
it('should fail on timeout', function(done){
setTimeout(done, 3000);
})

it('should pass test A after timeout', function(done){
setTimeout(done, 1000);
})
it('should pass run test B after timeout', function(done){
setTimeout(done, 1000);
})

it('should fail on timeout again', function(done){
setTimeout(done, 3000);
})
})

describe('timeouts', function(){

describe('timeouts', function(){
it('should fail on timeout', function(done){
setTimeout(done, 3000);
})

it('should pass run test A after timeout', function(done){
setTimeout(done, 1000);
})
})

describe('timeouts', function(){
it('should fail on timeout', function(done){
setTimeout(done, 3000);
})

it('should pass test A after timeout', function(done){
setTimeout(done, 1000);
})
})


})


// Known issue: #270
// This will currently fail and stop further execution of mocha entirely.
// describe('timeouts-beforeEach', function(){
// var waitTime = 2500;
// beforeEach(function(done){
// setTimeout(done, waitTime);
// waitTime -= 1000;
// })
//
// it('should error on timeout during before each', function(done){
//
// })
//
// it('should pass without on timeout during before each', function(done){
//
// })
// })
12 changes: 1 addition & 11 deletions test/acceptance/timeout.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@

describe('timeouts', function(){
beforeEach(function(done){
// uncomment
// setTimeout(done, 3000);
done();
})

it('should error on timeout', function(done){
// uncomment
// setTimeout(done, 3000);
done();
})

it('should allow overriding per-test', function(done){
this.timeout(1000);
setTimeout(function(){
done();
}, 300);
})

})