unexpected
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -592,2 +592,24 @@ // Copyright (c) 2013 Sune Simonsen <sune@we-knowhow.dk> | ||
expect.addAssertion('to be an array whose items satisfy', function (callback) { | ||
if ('function' !== typeof callback) { | ||
throw new Error('Assertions "to be an array whose items satisfy" expects a functions as argument'); | ||
} | ||
expect(this.obj, 'to be an array'); | ||
var errors = []; | ||
forEach(this.obj, function (item, index) { | ||
try { | ||
callback(item); | ||
} catch (e) { | ||
errors.push(' ' + index + ': ' + e.message.replace(/\n/, '\n ')); | ||
} | ||
}); | ||
if (errors.length > 0) { | ||
var message = 'failed expectation in ' + inspect(this.obj) + '\n' + | ||
errors.join('\n'); | ||
throw new Error(message); | ||
} | ||
}); | ||
Assertion.prototype.inspect = inspect; | ||
@@ -594,0 +616,0 @@ Assertion.prototype.eql = eql; |
{ | ||
"name": "unexpected", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -305,2 +305,30 @@ # Unexpected | ||
**array whose items satify**: will run an assertion function for each items in an array | ||
```js | ||
expect([0, 1, 2, 3, 4], 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
``` | ||
Using this assertion result in very detailed error reporting show in the below example: | ||
```js | ||
expect([[0, 1, 2], [4, '5', 6], [7, 8, '9']], 'to be an array whose items satisfy', function (arr) { | ||
expect(arr, 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
}); | ||
``` | ||
will output: | ||
``` | ||
failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ] | ||
1: failed expectation in [ 4, '5', 6 ] | ||
1: expected '5' to be a 'number' | ||
2: failed expectation in [ 7, 8, '9' ] | ||
2: expected '9' to be a 'number' | ||
``` | ||
## Print all registered assertions to the console | ||
@@ -307,0 +335,0 @@ |
@@ -573,2 +573,65 @@ /*global describe, it, expect*/ | ||
describe('to be an array whose items satisfy assertion', function () { | ||
it('only accepts a function', function () { | ||
expect(function () { | ||
expect([1,2,3], 'to be an array whose items satisfy'); | ||
}, 'to throw', 'Assertions "to be an array whose items satisfy" expects a functions as argument'); | ||
expect(function () { | ||
expect([1,2,3], 'to be an array whose items satisfy', 42); | ||
}, 'to throw', 'Assertions "to be an array whose items satisfy" expects a functions as argument'); | ||
}); | ||
it('only accepts arrays as the target object', function () { | ||
expect(function () { | ||
expect(42, 'to be an array whose items satisfy', function (item) {}); | ||
}, 'to throw', "expected 42 to be an 'array'"); | ||
}); | ||
it('asserts that the given callback does not throw for any items in the array', function () { | ||
expect([0,1,2,3], 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
expect(['0','1','2','3'], 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'not to be a number'); | ||
}); | ||
}); | ||
it('fails when the assertion fails', function () { | ||
expect(function () { | ||
expect(['0',1,'2','3'], 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'not to be a number'); | ||
}); | ||
}, 'to throw', /expected 1 not to be a 'number'/); | ||
}); | ||
it('provides a detailed report of where failures occur', function () { | ||
expect(function () { | ||
expect([0, 1, '2', 3, 4], 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
expect(item, 'to be less than', 4); | ||
}); | ||
}, 'to throw', | ||
"failed expectation in [ 0, 1, '2', 3, 4 ]\n" + | ||
" 2: expected '2' to be a 'number'\n" + | ||
" 4: expected 4 to be less than 4"); | ||
}); | ||
it('indents failure reports of nested assertions correctly', function () { | ||
expect(function () { | ||
expect([[0, 1, 2], [4, '5', 6], [7, 8, '9']], 'to be an array whose items satisfy', function (arr) { | ||
expect(arr, 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
}); | ||
}, 'to throw', | ||
"failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ]\n" + | ||
" 1: failed expectation in [ 4, '5', 6 ]\n" + | ||
" 1: expected '5' to be a 'number'\n" + | ||
" 2: failed expectation in [ 7, 8, '9' ]\n" + | ||
" 2: expected '9' to be a 'number'"); | ||
}); | ||
}); | ||
it('throws if the assertion does not exists', function () { | ||
@@ -575,0 +638,0 @@ expect(function () { |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
213297
6507
412
0