unexpected-sinon
Advanced tools
Comparing version 9.0.4 to 9.1.0
@@ -1,2 +0,2 @@ | ||
Passes if the "timeline" of all the calls of a set of spies [satisfy](http://unexpected.js.org/assertions/any/to-satisfy/) a given spec: | ||
Passes if the "timeline" of all the calls of a set of spies [satisfies](http://unexpected.js.org/assertions/any/to-satisfy/) a given spec: | ||
@@ -77,2 +77,36 @@ ```js | ||
Note that the individual arguments are matched with | ||
[`to satisfy`](http://unexpected.js.org/assertions/any/to-satisfy/) | ||
semantics, which means that objects are allowed to have more properties than you | ||
specify, so the following passes: | ||
```js | ||
var mySpy = sinon.spy().named('mySpy'); | ||
mySpy({foo: 123, bar: 456}); | ||
expect([ mySpy ], 'to have calls satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
``` | ||
If that's not what you want, consider using the `exhaustively` flag: | ||
```js | ||
expect([ mySpy ], 'to have calls exhaustively satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
``` | ||
```output | ||
expected [ mySpy ] to have calls exhaustively satisfying [ { args: [ ... ] } ] | ||
[ | ||
mySpy( | ||
{ | ||
foo: 123, | ||
bar: 456 // should be removed | ||
} | ||
) at theFunction (theFileName:xx:yy) | ||
] | ||
``` | ||
You can also specify expected calls as a function that performs them: | ||
@@ -79,0 +113,0 @@ |
@@ -40,2 +40,36 @@ Passes if all the calls of a spy [satisfy](http://unexpected.js.org/assertions/any/to-satisfy/) a given spec: | ||
Note that the individual arguments are matched with | ||
[`to satisfy`](http://unexpected.js.org/assertions/any/to-satisfy/) | ||
semantics, which means that objects are allowed to have more properties than you | ||
specify, so the following passes: | ||
```js | ||
var mySpy = sinon.spy().named('mySpy'); | ||
mySpy({foo: 123, bar: 456}); | ||
expect(mySpy, 'to have calls satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
``` | ||
If that's not what you want, consider using the `exhaustively` flag: | ||
```js | ||
expect(mySpy, 'to have calls exhaustively satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
``` | ||
```output | ||
expected mySpy to have calls exhaustively satisfying [ { args: [ ... ] } ] | ||
[ | ||
mySpy( | ||
{ | ||
foo: 123, | ||
bar: 456 // should be removed | ||
} | ||
) at theFunction (theFileName:xx:yy) | ||
] | ||
``` | ||
You can also specify expected calls as a function that performs them: | ||
@@ -42,0 +76,0 @@ |
@@ -38,3 +38,3 @@ --- | ||
``` | ||
$ npm install unexpected unexpected-sinon | ||
$ npm install --save-dev unexpected unexpected-sinon | ||
``` | ||
@@ -46,3 +46,3 @@ | ||
var expect = require('unexpected').clone(); | ||
expect.installPlugin(require('unexpected-sinon')); | ||
expect.use(require('unexpected-sinon')); | ||
``` | ||
@@ -52,3 +52,3 @@ | ||
Include the `unexpected.js` found at the lib directory of this | ||
Include the `unexpected-sinon.js` found at the lib directory of this | ||
repository. | ||
@@ -66,3 +66,3 @@ | ||
var expect = weknowhow.expect.clone(); | ||
expect.installPlugin(weknowhow.unexpectedSinon); | ||
expect.use(weknowhow.unexpectedSinon); | ||
``` | ||
@@ -77,3 +77,3 @@ | ||
var expect = unexpected.clone(); | ||
expect.installPlugin(unexpectedSinon); | ||
expect.use(unexpectedSinon); | ||
// Your code | ||
@@ -80,0 +80,0 @@ }); |
@@ -100,3 +100,3 @@ /*global location*/ | ||
name: 'spyCall', | ||
base: 'array-like', | ||
base: 'object', | ||
identify: isSpyCall, | ||
@@ -142,2 +142,8 @@ prefix: function (output, value) { | ||
identify: isSpyCalls, | ||
similar: function (a, b) { | ||
var baseType = this.baseType; | ||
return a.args && b.args && a.args.length === b.args.length && a.args.every(function (aItem, index) { | ||
return baseType.similar(aItem, b.args[index]); | ||
}); | ||
}, | ||
inspect: function (spyCalls, depth, output, inspect) { | ||
@@ -179,3 +185,3 @@ this.prefix(output); | ||
expect.addAssertion('<spyCall> to satisfy <any>', function (expect, subject, value) { | ||
expect.addAssertion('<spyCall> to [exhaustively] satisfy <any>', function (expect, subject, value) { | ||
var subjectType = expect.findTypeOf(subject); | ||
@@ -208,3 +214,3 @@ var spyArguments = toSpyArguments(subject.args); | ||
if (typeof value.args !== 'undefined') { | ||
return expect(spyArguments, 'to satisfy', value.args); | ||
return expect(spyArguments, 'to [exhaustively] satisfy', value.args); | ||
} | ||
@@ -326,3 +332,3 @@ }), | ||
expect.addAssertion('<spy|array> to have calls satisfying <function>', function (expect, subject, value) { | ||
expect.addAssertion('<spy|array> to have calls [exhaustively] satisfying <function>', function (expect, subject, value) { | ||
var spies = subject; | ||
@@ -385,6 +391,6 @@ if (!Array.isArray(spies)) { | ||
}; | ||
return expect(spies, 'to have calls satisfying', expectedSpyCallSpecs); | ||
return expect(spies, 'to have calls [exhaustively] satisfying', expectedSpyCallSpecs); | ||
}); | ||
expect.addAssertion('<spy|array> to have calls satisfying <array|object>', function (expect, subject, value) { | ||
expect.addAssertion('<spy|array> to have calls [exhaustively] satisfying <array|object>', function (expect, subject, value) { | ||
var spies = subject; | ||
@@ -435,3 +441,3 @@ if (!Array.isArray(spies)) { | ||
return expect(spyCalls, 'to satisfy', value); | ||
return expect(spyCalls, 'to [exhaustively] satisfy', value); | ||
}); | ||
@@ -438,0 +444,0 @@ |
{ | ||
"name": "unexpected-sinon", | ||
"version": "9.0.4", | ||
"version": "9.1.0", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -41,5 +41,5 @@ "keywords": [ | ||
"sinon": "1.16.1", | ||
"unexpected": "10.3.1", | ||
"unexpected": "10.5.1", | ||
"unexpected-documentation-site-generator": "^3.3.1" | ||
} | ||
} |
@@ -169,3 +169,35 @@ /*global unexpected*/ | ||
var mySpy = sinon.spy().named('mySpy'); | ||
mySpy({foo: 123, bar: 456}); | ||
expect([ mySpy ], 'to have calls satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
try { | ||
expect([ mySpy ], 'to have calls exhaustively satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
expect.fail(function (output) { | ||
output.error("expected:").nl(); | ||
output.code("expect([ mySpy ], 'to have calls exhaustively satisfying', [").nl(); | ||
output.code(" { args: [ { foo: 123 } ] }").nl(); | ||
output.code("]);").nl(); | ||
output.error("to throw"); | ||
}); | ||
} catch (e) { | ||
expect(e, "to have message", | ||
"expected [ mySpy ] to have calls exhaustively satisfying [ { args: [ ... ] } ]\n" + | ||
"\n" + | ||
"[\n" + | ||
" mySpy(\n" + | ||
" {\n" + | ||
" foo: 123,\n" + | ||
" bar: 456 // should be removed\n" + | ||
" }\n" + | ||
" ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
} | ||
try { | ||
var spy1 = sinon.spy().named('spy1'); | ||
@@ -332,3 +364,35 @@ var spy2 = sinon.spy().named('spy2'); | ||
var mySpy = sinon.spy().named('mySpy'); | ||
mySpy({foo: 123, bar: 456}); | ||
expect(mySpy, 'to have calls satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
try { | ||
expect(mySpy, 'to have calls exhaustively satisfying', [ | ||
{ args: [ { foo: 123 } ] } | ||
]); | ||
expect.fail(function (output) { | ||
output.error("expected:").nl(); | ||
output.code("expect(mySpy, 'to have calls exhaustively satisfying', [").nl(); | ||
output.code(" { args: [ { foo: 123 } ] }").nl(); | ||
output.code("]);").nl(); | ||
output.error("to throw"); | ||
}); | ||
} catch (e) { | ||
expect(e, "to have message", | ||
"expected mySpy to have calls exhaustively satisfying [ { args: [ ... ] } ]\n" + | ||
"\n" + | ||
"[\n" + | ||
" mySpy(\n" + | ||
" {\n" + | ||
" foo: 123,\n" + | ||
" bar: 456 // should be removed\n" + | ||
" }\n" + | ||
" ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
} | ||
try { | ||
var increment = sinon.spy().named('increment'); | ||
@@ -640,2 +704,61 @@ increment(1); | ||
it("assertions/spyCall/to-satisfy.md contains correct examples", function () { | ||
var testPromises = []; | ||
var decrement = sinon.spy(function decrement(n) { | ||
return n - 1; | ||
}); | ||
decrement(42); | ||
decrement(46); | ||
expect(decrement.firstCall, 'to satisfy', { args: [ 42 ], returned: 41 }); | ||
try { | ||
var decrement = sinon.spy(function decrement(n) { | ||
return n - 1; | ||
}).named('decrement'); | ||
decrement(20); | ||
decrement(200); | ||
decrement(2000); | ||
expect(decrement.secondCall, 'to satisfy', { args: [ 20 ] }); | ||
expect.fail(function (output) { | ||
output.error("expected:").nl(); | ||
output.code("var decrement = sinon.spy(function decrement(n) {").nl(); | ||
output.code(" return n - 1;").nl(); | ||
output.code("}).named('decrement');").nl(); | ||
output.code("").nl(); | ||
output.code("decrement(20);").nl(); | ||
output.code("decrement(200);").nl(); | ||
output.code("decrement(2000);").nl(); | ||
output.code("").nl(); | ||
output.code("expect(decrement.secondCall, 'to satisfy', { args: [ 20 ] });").nl(); | ||
output.error("to throw"); | ||
}); | ||
} catch (e) { | ||
expect(e, "to have message", | ||
"expected decrement( 200 ) at theFunction (theFileName:xx:yy)\n" + | ||
"to satisfy { args: [ 20 ] }\n" + | ||
"\n" + | ||
"decrement(\n" + | ||
" 200 // should equal 20\n" + | ||
") at theFunction (theFileName:xx:yy)" | ||
); | ||
} | ||
var getRandomPrefixedInteger = sinon.spy(function getRandomPrefixedInteger() { | ||
return 'prefix-' + parseInt(Math.random() * 10, 10); | ||
}); | ||
getRandomPrefixedInteger(); | ||
getRandomPrefixedInteger(); | ||
getRandomPrefixedInteger(); | ||
expect(getRandomPrefixedInteger.getCall(0), 'to satisfy', { | ||
returned: expect.it('to be a string').and('to match', /^prefix-[0-9]$/) | ||
}); | ||
return expect.promise.all(testPromises); | ||
}); | ||
it("index.md contains correct examples", function () { | ||
@@ -642,0 +765,0 @@ var testPromises = []; |
@@ -828,2 +828,28 @@ /*global describe, it, beforeEach, sinon, unexpected*/ | ||
describe('with the exhaustively flag', function () { | ||
it('should fail if an object parameter contains additional properties', function () { | ||
spy({foo: 123}, [{bar: 'quux'}]); | ||
return expect(function () { | ||
expect(spy, 'to have calls exhaustively satisfying', [ | ||
{ args: [{}, [{}]] } | ||
]); | ||
}, 'to throw', | ||
"expected spy1 to have calls exhaustively satisfying [ { args: [ ..., ... ] } ]\n" + | ||
"\n" + | ||
"[\n" + | ||
" spy1(\n" + | ||
" {\n" + | ||
" foo: 123 // should be removed\n" + | ||
" },\n" + | ||
" [\n" + | ||
" {\n" + | ||
" bar: 'quux' // should be removed\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
}); | ||
}); | ||
describe('when providing the expected calls as a function', function () { | ||
@@ -888,3 +914,3 @@ it('should succeed', function () { | ||
it('should render missing spy calls nicely', function () { | ||
it('should render a spy call missing at the end', function () { | ||
spy('abc', true); | ||
@@ -911,2 +937,87 @@ | ||
it('should render a spy call missing at the end', function () { | ||
spy('abc', true); | ||
expect(function () { | ||
expect(spy, 'to have calls satisfying', function () { | ||
spy('def', false); | ||
spy('abc', true); | ||
}); | ||
}, 'to throw', | ||
"expected spy1 to have calls satisfying\n" + | ||
"[\n" + | ||
" spy1( 'def', false )\n" + | ||
" spy1( 'abc', true )\n" + | ||
"]\n" + | ||
"\n" + | ||
"[\n" + | ||
" // missing spy1( 'def', false )\n" + | ||
" spy1( 'abc', true ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
}); | ||
it('should render a spy call missing in the middle', function () { | ||
spy(123, 456); | ||
spy(234); | ||
spy(987); | ||
expect(function () { | ||
expect(spy, 'to have calls satisfying', function () { | ||
spy(123, 456); | ||
spy(false); | ||
spy(234); | ||
spy(987); | ||
}); | ||
}, 'to throw', | ||
"expected spy1 to have calls satisfying\n" + | ||
"[\n" + | ||
" spy1( 123, 456 )\n" + | ||
" spy1( false )\n" + | ||
" spy1( 234 )\n" + | ||
" spy1( 987 )\n" + | ||
"]\n" + | ||
"\n" + | ||
"[\n" + | ||
" spy1( 123, 456 ) at theFunction (theFileName:xx:yy)\n" + | ||
" // missing spy1( false )\n" + | ||
" spy1( 234 ) at theFunction (theFileName:xx:yy)\n" + | ||
" spy1( 987 ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
}); | ||
it('should render the minimal diff when a structurally similar spy call is followed by an extraneous one', function () { | ||
spy(123, 456); | ||
spy({ foo: 123 }); | ||
spy(456); | ||
spy(987); | ||
expect(function () { | ||
expect(spy, 'to have calls satisfying', function () { | ||
spy(123, 456); | ||
spy({ foo: 456 }); | ||
spy(987); | ||
}); | ||
}, 'to throw', | ||
"expected spy1 to have calls satisfying\n" + | ||
"[\n" + | ||
" spy1( 123, 456 )\n" + | ||
" spy1( { foo: 456 } )\n" + | ||
" spy1( 987 )\n" + | ||
"]\n" + | ||
"\n" + | ||
"[\n" + | ||
" spy1( 123, 456 ) at theFunction (theFileName:xx:yy)\n" + | ||
" spy1(\n" + | ||
" {\n" + | ||
" foo: 123 // should equal 456\n" + | ||
" }\n" + | ||
" ) at theFunction (theFileName:xx:yy)\n" + | ||
" spy1( 456 ) at theFunction (theFileName:xx:yy) // should be removed\n" + | ||
" spy1( 987 ) at theFunction (theFileName:xx:yy)\n" + | ||
"]" | ||
); | ||
}); | ||
it('should work with expect.it', function () { | ||
@@ -913,0 +1024,0 @@ spy('abc', true); |
695868
29
6911