assert-the-unexpected
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -108,3 +108,3 @@ var AssertionError = require('assert').AssertionError; | ||
function createWrappedExpect(localExpect) { | ||
function createWrappedExpect(localExpect, options) { | ||
return function wrappedExpect() { | ||
@@ -116,3 +116,3 @@ var args = Array.prototype.slice.apply(arguments); | ||
} catch (err) { | ||
throw new UnexpectedAssertError(err); | ||
throw new UnexpectedAssertError(err, options); | ||
} | ||
@@ -149,8 +149,13 @@ }; | ||
expect.errorMode = 'bubble'; | ||
expect(a, '[not] to equal', b); | ||
}); | ||
var wrappedExpect = createWrappedExpect(expectWithLooseEquality); | ||
var baseWrappedExpect = createWrappedExpect(expectWithLooseEquality); | ||
function wrappedWithMessage(message) { | ||
return createWrappedExpect(expectWithLooseEquality, { | ||
message: message | ||
}); | ||
} | ||
function checkIfError(value) { | ||
@@ -164,15 +169,18 @@ try { | ||
function checkNotThrow(block, errorConstraint) { | ||
function checkNotThrow(block, errorConstraint, message) { | ||
function checkError(blockError) { | ||
if (isString(errorConstraint)) { | ||
var outputMessage = 'Got unwanted exception'; | ||
var outputMessageSuffix; | ||
if (isRegExp(errorConstraint)) { | ||
try { | ||
expect(blockError, 'to have message', errorConstraint); | ||
} catch (e) { | ||
throw new UnexpectedAssertError(blockError, { | ||
message: 'Got unwanted exception. ' + errorConstraint | ||
}); | ||
if (!isString(message)) { | ||
throw blockError; | ||
} | ||
outputMessageSuffix = '. ' + message; | ||
} | ||
} | ||
if (errorConstraint) { | ||
} else if (errorConstraint) { | ||
/* | ||
@@ -186,13 +194,30 @@ * Check whether the unexpected exception satisfied | ||
} catch (e) { | ||
// it was satisfied so throw an AssertionError | ||
throw new UnexpectedAssertError(blockError); | ||
outputMessageSuffix = ' (' + errorConstraint.name + ').'; | ||
if (isString(message)) { | ||
outputMessageSuffix += ' ' + message; | ||
} else { | ||
outputMessageSuffix += '.'; | ||
} | ||
} | ||
// it was not satisfied so throw the orignal error | ||
throw blockError; | ||
if (isString(errorConstraint)) { | ||
outputMessageSuffix = '. ' + errorConstraint; | ||
} | ||
if (!outputMessageSuffix) { | ||
// it was not satisfied so throw the orignal error | ||
throw blockError; | ||
} | ||
} | ||
if (outputMessageSuffix) { | ||
outputMessage += outputMessageSuffix; | ||
} else { | ||
outputMessage += '..'; | ||
} | ||
// notify an eception was seen in the absence of a constraint | ||
throw new UnexpectedAssertError(blockError, { | ||
message: 'Got unwanted exception..' | ||
message: outputMessage | ||
}); | ||
@@ -210,3 +235,3 @@ } | ||
function checkThrows(block, errorConstraint) { | ||
function checkThrows(block, errorConstraint, message) { | ||
function checkError(blockError) { | ||
@@ -234,5 +259,9 @@ if (isRegExp(blockError)) { | ||
var checkToError; | ||
var outputMessageSuffix; | ||
if (isRegExp(errorConstraint)) { | ||
checkToError = errorConstraint; | ||
// in this case a user defined messagee may be the next arg | ||
outputMessageSuffix = message; | ||
} else { | ||
@@ -244,2 +273,5 @@ checkToError = function (e) { | ||
}; | ||
// the message is the second argument if it exists | ||
outputMessageSuffix = errorConstraint; | ||
} | ||
@@ -254,3 +286,8 @@ | ||
// unpack an unexpected error | ||
throw e.originalError; | ||
e = e.originalError; | ||
// throw original error on regex mismatch | ||
if (isRegExp(errorConstraint)) { | ||
throw e; | ||
} | ||
} else if (e.name !== 'UnexpectedError') { | ||
@@ -263,4 +300,4 @@ // throw any errors that do not arise from "to error" | ||
if (isString(errorConstraint)) { | ||
outputMessage += ' ' + errorConstraint; | ||
if (isString(outputMessageSuffix)) { | ||
outputMessage += ' ' + outputMessageSuffix; | ||
} else { | ||
@@ -277,3 +314,5 @@ outputMessage += '.'; | ||
function checkTruthy(value) { | ||
function checkTruthy(value, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(value, 'to be truthy'); | ||
@@ -358,7 +397,9 @@ } | ||
return assignObject(function (value) { | ||
checkTruthy(value); | ||
return assignObject(function assertTheUnexpected(value, message) { | ||
checkTruthy(value, message); | ||
}, { | ||
AssertionError: UnexpectedAssertError, | ||
deepEqual: function deepEqual(actual, expected) { | ||
deepEqual: function deepEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'to loosely equal', expected); | ||
@@ -371,3 +412,5 @@ | ||
}, | ||
deepStrictEqual: function deepStrictEqual(actual, expected) { | ||
deepStrictEqual: function deepStrictEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'to equal', expected); | ||
@@ -384,6 +427,8 @@ | ||
}, | ||
doesNotThrow: function (block, expected) { | ||
checkNotThrow(block, expected); | ||
doesNotThrow: function doesNotThrow(block, expected, message) { | ||
checkNotThrow(block, expected, message); | ||
}, | ||
equal: function equal(actual, expected) { | ||
equal: function equal(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'to loosely be', expected); | ||
@@ -394,22 +439,32 @@ }, | ||
}, | ||
notEqual: function notEqual(actual, expected) { | ||
notEqual: function notEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'not to loosely be', expected); | ||
}, | ||
notDeepEqual: function notDeepEqual(actual, expected) { | ||
notDeepEqual: function notDeepEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'not to loosely equal', expected); | ||
}, | ||
notDeepStrictEqual: function notDeepStrictEqual(actual, expected) { | ||
notDeepStrictEqual: function notDeepStrictEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'not to equal', expected); | ||
}, | ||
notStrictEqual: function notStrictEqual(actual, expected) { | ||
notStrictEqual: function notStrictEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'not to be', expected); | ||
}, | ||
ok: function (value) { | ||
checkTruthy(value); | ||
ok: function ok(value, message) { | ||
checkTruthy(value, message); | ||
}, | ||
strictEqual: function strictEqual(actual, expected) { | ||
strictEqual: function strictEqual(actual, expected, message) { | ||
var wrappedExpect = message ? wrappedWithMessage(message) : baseWrappedExpect; | ||
wrappedExpect(actual, 'to be', expected); | ||
}, | ||
throws: function (block, expected) { | ||
checkThrows(block, expected); | ||
throws: function throws(block, expected, message) { | ||
checkThrows(block, expected, message); | ||
} | ||
@@ -416,0 +471,0 @@ }); |
{ | ||
"name": "assert-the-unexpected", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "assert facade for Unexpected", | ||
@@ -20,3 +20,7 @@ "author": "Alex J Burke <alex@alexjeffburke.com>", | ||
"unexpected": "^10.26.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~3.2.0", | ||
"semver": "~5.3.0" | ||
} | ||
} |
# assert-the-unexpected | ||
[![NPM version](https://badge.fury.io/js/assert-the-unexpected.svg)](http://badge.fury.io/js/assert-the-unexpected) | ||
[![Build Status](https://travis-ci.org/alexjeffburke/assert-the-unexpected.svg?branch=master)](https://travis-ci.org/alexjeffburke/assert-the-unexpected) | ||
This implements the node core `assert` API as an Unexpected facade. | ||
A great deal of emphasis was placed on compatibility and this module aims to | ||
be a drop-in replacement. It was developed against the node 4.7.3 test suite | ||
in core which it passes with the exception of the following: | ||
be a drop-in replacement. It was developed against the node core test suite | ||
which it passes with the exception of the following: | ||
* circular references are not supported | ||
@@ -12,1 +15,3 @@ * deepEqual() **not** throwing on comparisons of: | ||
- mismatching prototypes | ||
The current baseline version of node assert is **4.7.3**. |
@@ -5,2 +5,8 @@ var assert = require('../lib/assertTheUnexpected'); | ||
describe('assertTheUnexpected', function () { | ||
function hasAssertionErrorMessage(err, message) { | ||
message = 'AssertionError: ' + message; | ||
expect(err.toString(), 'to equal', message); | ||
} | ||
describe('deepEqual', function () { | ||
@@ -66,2 +72,12 @@ it('should compare objects with loosely equal property (lhs)', function () { | ||
}); | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.deepEqual([0], [true], theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
@@ -76,3 +92,3 @@ | ||
it('should include a string constaint on an unwanted exception', function () { | ||
it('should throw and include user defined message', function () { | ||
expect(function () { | ||
@@ -88,2 +104,32 @@ assert.doesNotThrow(function () { throw new Error('boo'); }, 'hoo'); | ||
}); | ||
it('should respect a user defined message on regex mismatch', function () { | ||
var theMessage = 'ouch'; | ||
expect(function () { | ||
assert.doesNotThrow(function () { throw new Error('boo'); }, /hoo/, theMessage); | ||
}, 'to error', 'Got unwanted exception. ' + theMessage); | ||
}); | ||
it('should throw an assertion error on Error type match', function () { | ||
var theMessage = 'ouch'; | ||
expect(function () { | ||
assert.doesNotThrow(function () { throw new Error('boo'); }, Error); | ||
}, 'to error', 'Got unwanted exception (Error)..'); | ||
}); | ||
it('should throw the original error on Error type mismatch', function () { | ||
expect(function () { | ||
assert.doesNotThrow(function () { throw new Error('boo'); }, assert.AssertionError); | ||
}, 'to error', 'boo'); | ||
}); | ||
it('should throw an assertion error on Error type match', function () { | ||
var theMessage = 'ouch'; | ||
expect(function () { | ||
assert.doesNotThrow(function () { throw new Error('boo'); }, Error, theMessage); | ||
}, 'to error', 'Got unwanted exception (Error). ' + theMessage); | ||
}); | ||
}); | ||
@@ -115,2 +161,12 @@ | ||
}); | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.equal('a', 'b', theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
@@ -132,2 +188,11 @@ | ||
}); | ||
it('should ignore a user supplied message', function () { | ||
var theError = new Error(); | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.ifError(theError, theMessage); | ||
}, 'to error', theError); | ||
}); | ||
}); | ||
@@ -141,4 +206,80 @@ | ||
}); | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.notEqual(0, '0', theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('notDeepEqual', function () { | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.notDeepEqual([0], ['0'], theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('notDeepStrictEqual', function () { | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.notDeepStrictEqual([0], [0], theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('notStrictEqual', function () { | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.notStrictEqual(0, 0, theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('ok', function () { | ||
it('should throw on falsy value', function () { | ||
expect(function () { | ||
assert.ok(false); | ||
}, 'to error', /expected/); | ||
}); | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.ok(false, theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('strictEqual', function () { | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.strictEqual('boo', 'hoo', theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
describe('throws', function () { | ||
@@ -151,3 +292,3 @@ it('should pass on seeing an exception', function () { | ||
it('should throw on a missing expection', function () { | ||
it('should throw on a missing exception', function () { | ||
expect(function () { | ||
@@ -158,3 +299,3 @@ assert.throws(function () {}); | ||
it('should throw and include the string constraint', function () { | ||
it('should throw and include user defined message', function () { | ||
expect(function () { | ||
@@ -165,8 +306,16 @@ assert.throws(function () {}, 'hoo'); | ||
it('should throw the string constraint on string mismatch', function () { | ||
it('should throw on a missing exception and regex', function () { | ||
expect(function () { | ||
assert.throws(function () { new Error('boo'); }, 'hoo'); | ||
}, 'to error', 'Missing expected exception. hoo'); | ||
assert.throws(function () {}, /hoo/); | ||
}, 'to error', 'Missing expected exception..'); | ||
}); | ||
it('should throw on missing exception with regex and message', function () { | ||
var theMessage = 'ouch'; | ||
expect(function () { | ||
assert.throws(function () {}, /hoo/, theMessage); | ||
}, 'to error', 'Missing expected exception. ' + theMessage); | ||
}); | ||
it('should throw the original error on regex mismatch', function () { | ||
@@ -177,3 +326,27 @@ expect(function () { | ||
}); | ||
it('should ignore a user supplied message on regex mismatch', function () { | ||
expect(function () { | ||
assert.throws(function () { throw new Error('boo'); }, /hoo/, 'ouch'); | ||
}, 'to error', 'boo'); | ||
}); | ||
}); | ||
describe('when used a plain function', function () { | ||
it('should throw the', function () { | ||
expect(function () { | ||
assert.ok(false); | ||
}, 'to error', /expected/); | ||
}); | ||
it('should respect a user supplied message', function () { | ||
var theMessage = 'oh dear'; | ||
expect(function () { | ||
assert.ok(false, theMessage); | ||
}, 'to error', function (e) { | ||
hasAssertionErrorMessage(e, theMessage); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,5 +1,17 @@ | ||
var semver = require('semver'); | ||
describe('node', function () { | ||
it('should pass node 4.7.3 tests', function () { | ||
function itByVersion(version, description, block) { | ||
var isMinimumVersion = semver.gte(process.version, version); | ||
if (isMinimumVersion) { | ||
return it(description, block); | ||
} else { | ||
return it.skip(description, block); | ||
} | ||
} | ||
itByVersion('4.0.0', 'should pass node 4.7.3 tests', function () { | ||
require('./node/node-4.7.3'); | ||
}); | ||
}); |
'use strict'; | ||
var assert = require('assert'); | ||
var a = require('../../lib/assertTheUnexpected'); | ||
var semver = require('semver'); | ||
@@ -532,3 +533,10 @@ function makeBlock(f) { | ||
} catch (e) { | ||
assert.equal(e.toString(), 'TypeError: block must be a function'); | ||
var expectedMessage; | ||
if (semver.gte(process.version, '6.0.0')) { | ||
expectedMessage = 'TypeError: "block" argument must be a function'; | ||
} else { | ||
expectedMessage = 'TypeError: block must be a function'; | ||
} | ||
assert.equal(e.toString(), expectedMessage); | ||
} | ||
@@ -535,0 +543,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43715
10
1143
17
2