-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from ljharb/throws_non_function_should_fail
[Fix] Ensure that non-functions passed to `throws` fail the test, just like `assert`
- Loading branch information
Showing
2 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,135 @@ | ||
var test = require('../'); | ||
var tape = require('../'); | ||
var tap = require('tap'); | ||
var concat = require('concat-stream'); | ||
|
||
function fn() { | ||
throw new TypeError('RegExp'); | ||
} | ||
|
||
test('throws', function (t) { | ||
function getNonFunctionMessage(fn) { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
return e.message; | ||
} | ||
} | ||
|
||
tape('throws', function (t) { | ||
t.throws(fn); | ||
t.end(); | ||
}); | ||
|
||
test('throws (RegExp match)', function (t) { | ||
tape('throws (RegExp match)', function (t) { | ||
t.throws(fn, /RegExp/); | ||
t.end(); | ||
}); | ||
|
||
test('throws (Function match)', function (t) { | ||
tape('throws (Function match)', function (t) { | ||
t.throws(fn, TypeError); | ||
t.end(); | ||
}); | ||
|
||
tap.test('failures', function (tt) { | ||
tt.plan(1); | ||
|
||
var test = tape.createHarness(); | ||
test.createStream().pipe(concat(function (body) { | ||
tt.equal( | ||
body.toString('utf8'), | ||
'TAP version 13\n' | ||
+ '# non functions\n' | ||
+ 'not ok 1 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 2 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 3 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 4 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 5 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 6 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 7 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n" | ||
+ ' ...\n' | ||
+ 'not ok 8 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: |-\n' | ||
+ ' undefined\n' | ||
+ ' actual: |-\n' | ||
+ " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n" | ||
+ ' ...\n' | ||
+ '# function\n' | ||
+ 'not ok 9 should throw\n' | ||
+ ' ---\n' | ||
+ ' operator: throws\n' | ||
+ ' expected: undefined\n' | ||
+ ' actual: undefined\n' | ||
+ ' ...\n\n' | ||
+ '1..9\n' | ||
+ '# tests 9\n' | ||
+ '# pass 0\n' | ||
+ '# fail 9\n' | ||
); | ||
})); | ||
|
||
test('non functions', function (t) { | ||
t.plan(8); | ||
t.throws(); | ||
t.throws(null); | ||
t.throws(true); | ||
t.throws(false); | ||
t.throws('abc'); | ||
t.throws(/a/g); | ||
t.throws([]); | ||
t.throws({}); | ||
}); | ||
|
||
test('function', function (t) { | ||
t.plan(1); | ||
t.throws(function () {}); | ||
}); | ||
}); |