UNPKG

15.9 kBMarkdownView Raw
1# referee-sinon
2
3Sinon.JS and the referee assertion library in one package.
4
5
6## Usage
7
8```shell
9npm install @sinonjs/referee-sinon --save-dev
10```
11
12Note that you don't need to install `@sinonjs/referee` or `sinon`.
13
14```js
15const referee = require("@sinonjs/referee-sinon");
16
17const assert = referee.assert
18const refute = referee.refute
19const sinon = referee.sinon
20```
21
22Or, [if you can make use][compat] of [destructuring assignments][mdn]:
23
24```js
25const { assert, refute, sinon } = require("@sinonjs/referee-sinon");
26```
27
28[compat]: http://kangax.github.io/compat-table/es6/#test-destructuring
29[mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
30
31## Sinon
32
33The exposed `sinon` object is the full Sinon.JS API as [documented on the Sinon.JS homepage](http://sinonjs.org).
34
35## Assertions
36
37The descriptions are for `assert`, but the corresponding failure messages for `refute` are also mentioned. For refute the behaviour is exactly opposite.
38
39*Overview:*
40
41* [`called()`](#called)
42* [`callCount()`](#callcount)
43* [`callOrder()`](#callorder)
44* [`calledOnce()`](#calledonce)
45* [`calledTwice()`](#calledtwice)
46* [`calledThrice()`](#calledthrice)
47* [`calledOn()`](#calledon)
48* [`alwaysCalledOn()`](#alwayscalledon)
49* [`calledWith()`](#calledwith)
50* [`calledWithNew()`](#calledwithnew)
51* [`alwaysCalledWith()`](alwayscalledwith)
52* [`alwaysCalledWithNew()`](alwayscalledwithnew)
53* [`calledOnceWith()`](#calledoncewith)
54* [`calledWithExactly()`](#calledwithexactly)
55* [`alwaysCalledWithExactly()`](#alwayscalledwithexactly)
56* [`threw()`](#threw)
57* [`alwaysThrew()`](#alwaysthrew)
58
59### `called()`
60
61```js
62assert.called(spy)
63```
64
65Fails if the `spy` has never been called.
66
67```js
68var spy = this.spy();
69
70assert.called(spy); // Fails
71
72spy();
73assert.called(spy); // Passes
74
75spy();
76assert.called(spy); // Passes
77```
78
79#### Messages
80
81```js
82assert.called.message = "Expected ${0} to be called at least once but was never called";
83```
84
85<dl>
86 <dt>`${0}`:</dt>
87 <dd>The spy</dd>
88</dl>
89
90```js
91refute.called.message = "Expected ${0} to not be called but was called ${1}${2}";
92```
93
94
95<dl>
96 <dt>`${0}`:</dt>
97 <dd>The spy</dd>
98 <dt>`${1}`:</dt>
99 <dd>The number of calls as a string. Ex: “two times”</dd>
100 <dt>`${2}`:</dt>
101 <dd>All calls formatted as a multi-line string</dd>
102</dl>
103
104### `callCount()`
105
106```js
107assert.callCount(spy, count)
108```
109
110Fails if the `spy`'s `callCount` property is not exactly `count`
111
112```js
113var spy = this.spy();
114
115assert.callCount(spy, 0); // Passes
116assert.callCount(spy, 1); // Fails
117
118spy();
119assert.callCount(spy, 0); // Fails
120assert.callCount(spy, 1); // Passes
121```
122
123#### Messages
124
125```js
126assert.called.message = "Expected ${spyObj} to be called exactly ${expectedTimes} times, but was called ${actualTimes}";
127refute.called.message = "Expected ${spyObj} to not be called exactly ${expectedTimes} times"
128```
129
130<dl>
131 <dt>`${spyObj}`:</dt>
132 <dd>The spy</dd>
133 <dt>`${expectedTimes}`:</dt>
134 <dd>The expected number of calls</dd>
135 <dt>`${actualTimes}`:</dt>
136 <dd>The actual number of calls</dd>
137</dl>
138
139
140### `callOrder()`
141
142```js
143assert.callOrder(spy, spy2, ...)
144```
145
146Fails if the spies were not called in the specified order.
147
148```js
149var spy1 = this.spy();
150var spy2 = this.spy();
151var spy3 = this.spy();
152
153spy1();
154spy2();
155spy3();
156
157assert.callOrder(spy1, spy3, spy2); // Fails
158assert.callOrder(spy1, spy2, spy3); // Passes
159```
160
161#### Messages
162
163```js
164assert.callOrder.message = "Expected ${expected} to be called in order but were called as ${actual}";
165refute.callOrder.message = "Expected ${expected} not to be called in order";
166```
167
168<dl>
169 <dt>`${expected}`:</dt>
170 <dd>A string representation of the expected call order</dd>
171 <dt>`${actual}`:</dt>
172 <dd>A string representation of the actual call order</dd>
173</dl>
174
175
176### `calledOnce()`
177
178```js
179assert.calledOnce(spy)
180```
181
182Fails if the `spy` has never been called or if it was called more than once.
183
184```js
185var spy = this.spy();
186
187assert.calledOnce(spy); // Fails
188
189spy();
190assert.calledOnce(spy); // Passes
191
192spy();
193assert.calledOnce(spy); // Fails
194```
195
196#### Messages
197
198```
199assert.calledOnce.message = "Expected ${0} to be called once but was called ${1}${2}";
200refute.calledOnce.message = "Expected ${0} to not be called exactly once${2}";
201```
202
203<dl>
204 <dt>`${0}`:</dt>
205 <dd>The spy</dd>
206 <dt>`${1}`:</dt>
207 <dd>The number of calls, as a string. Ex: “two times”</dd>
208 <dt>`${2}`:</dt>
209 <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
210</dl>
211
212### `calledTwice()`
213
214```js
215assert.calledTwice(spy)
216```
217
218Only passes if the `spy` was called exactly twice.
219
220```js
221var spy = this.spy();
222
223assert.calledTwice(spy); // Fails
224
225spy();
226assert.calledTwice(spy); // Fails
227
228spy();
229assert.calledTwice(spy); // Passes
230
231spy();
232assert.calledTwice(spy); // Fails
233```
234
235#### Messages
236
237```js
238assert.calledTwice.message = "Expected ${0} to be called twice but was called ${1}${2}";
239refute.calledTwice.message = "Expected ${0} to not be called exactly twice${2}";
240```
241
242<dl>
243 <dt>`${0}`:</dt>
244 <dd>The spy</dd>
245 <dt>`${1}`:</dt>
246 <dd>The number of calls, as a string. Ex: “two times”</dd>
247 <dt>`${2}`:</dt>
248 <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
249</dl>
250
251### `calledThrice()`
252
253```js
254assert.calledThrice(spy)
255```
256
257Only passes if the `spy` has been called exactly three times.
258
259```js
260var spy = this.spy();
261
262assert.calledThrice(spy); // Fails
263
264spy();
265assert.calledThrice(spy); // Fails
266
267spy();
268assert.calledThrice(spy); // Fails
269
270spy();
271assert.calledThrice(spy); // Passes
272
273spy();
274assert.calledThrice(spy); // Fails
275```
276
277#### Messages
278
279```js
280assert.calledThrice.message = "Expected ${0} to be called thrice but was called ${1}${2}";
281refute.calledThrice.message = "Expected ${0} to not be called exactly thrice${2}";
282```
283
284<dl>
285 <dt>`${0}`:</dt>
286 <dd>The spy</dd>
287 <dt>`${1}`:</dt>
288 <dd>The number of calls, as a string. Ex: “two times”</dd>
289 <dt>`${2}`:</dt>
290 <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
291</dl>
292
293### `calledOn()`
294
295```js
296assert.calledOn(spy, obj)
297```
298
299Passes if the `spy` was called at least once with `obj` as its `this` value.
300
301```js
302var spy = this.spy();
303var obj1 = {};
304var obj2 = {};
305var obj3 = {};
306
307spy.call(obj2);
308spy.call(obj3);
309
310assert.calledOn(spy, obj1); // Fails
311assert.calledOn(spy, obj2); // Passes
312assert.calledOn(spy, obj3); // Passes
313```
314
315#### Messages
316
317```js
318assert.calledOn.message = "Expected ${0} to be called with ${1} as this but was called on ${2}";
319refute.calledOn.message = "Expected ${0} not to be called with ${1} as this";
320```
321
322<dl>
323 <dt>`${0}`:</dt>
324 <dd>The spy</dd>
325 <dt>`${1}`:</dt>
326 <dd>The object obj which is expected to have been this at least once</dd>
327 <dt>`${2}`:</dt>
328 <dd>List of objects which actually have been `this`</dd>
329</dl>
330
331### `alwaysCalledOn()`
332
333```js
334assert.alwaysCalledOn(spy, obj)
335```
336
337Passes if the `spy` was always called with `obj` as its `this` value.
338
339```js
340var spy1 = this.spy();
341var spy2 = this.spy();
342var obj1 = {};
343var obj2 = {};
344
345spy1.call(obj1);
346spy1.call(obj2);
347
348spy2.call(obj2);
349spy2.call(obj2);
350
351assert.alwaysCalledOn(spy1, obj1); // Fails
352assert.alwaysCalledOn(spy1, obj2); // Fails
353assert.alwaysCalledOn(spy2, obj1); // Fails
354assert.alwaysCalledOn(spy2, obj2); // Passes
355```
356
357#### Messages
358
359```js
360assert.alwaysCalledOn.message = "Expected ${0} to always be called with ${1} as this but was called on ${2}";
361refute.alwaysCalledOn.message = "Expected ${0} not to always be called with ${1} as this";
362```
363
364<dl>
365 <dt>`${0}`:</dt>
366 <dd>The spy</dd>
367 <dt>`${1}`:</dt>
368 <dd>The object obj which is expected always to have been `this`</dd>
369 <dt>`${2}`:</dt>
370 <dd>List of objects which actually have been `this`</dd>
371</dl>
372
373
374### `calledWith()`
375
376```js
377assert.calledWith(spy, arg1, arg2, ...)
378```
379
380Passes if the `spy` was called at least once with the specified arguments. Other arguments may have been passed after the specified ones.
381
382```js
383var spy = this.spy();
384var arr = [1, 2, 3];
385spy(12);
386spy(42, 13);
387spy("Hey", arr, 2);
388
389assert.calledWith(spy, 12); // Passes
390assert.calledWith(spy, "Hey"); // Passes
391assert.calledWith(spy, "Hey", 12); // Fails
392assert.calledWith(spy, "Hey", arr); // Passes
393```
394
395#### Messages
396
397```js
398assert.calledWith.message = "Expected ${0} to be called with arguments ${1}${2}";
399refute.calledWith.message = "Expected ${0} not to be called with arguments ${1}${2}";
400```
401
402<dl>
403 <dt>`${0}`:</dt>
404 <dd>The spy</dd>
405 <dt>`${1}`:</dt>
406 <dd>The expected arguments</dd>
407 <dt>`${2}`:</dt>
408 <dd>String representation of all calls</dd>
409</dl>
410
411### `calledWithNew()`
412
413```js
414assert.calledWithNew(spy)
415```
416
417Fails if the `spy` has never called with `new`.
418
419```js
420var spy = this.spy();
421
422assert.calledWithNew(spy); // Fails
423
424new spy();
425assert.calledWithNew(spy); // Passes
426
427spy();
428assert.calledWithNew(spy); // Passes
429```
430
431#### Messages
432
433```js
434assert.calledWithNew.message = "Expected ${spyObj} to be called with 'new' at least once but was never called with 'new'";
435refute.calledWithNew.message = "Expected ${spyObj} to not be called with 'new'";
436```
437
438<dl>
439 <dt>`${spyObj}`:</dt>
440 <dd>The spy</dd>
441</dl>
442
443
444### `alwaysCalledWith()`
445
446```js
447assert.alwaysCalledWith(spy, arg1, arg2, ...)
448```
449
450Passes if the `spy` was always called with the specified arguments. Other arguments may have been passed after the specified ones.
451
452```js
453var spy = this.spy();
454var arr = [1, 2, 3];
455spy("Hey", arr, 12);
456spy("Hey", arr, 13);
457
458assert.alwaysCalledWith(spy, "Hey"); // Passes
459assert.alwaysCalledWith(spy, "Hey", arr); // Passes
460assert.alwaysCalledWith(spy, "Hey", arr, 12); // Fails
461```
462
463#### Messages
464
465```js
466assert.alwaysCalledWith.message = "Expected ${0} to always be called with arguments ${1}${2}";
467refute.alwaysCalledWith.message = "Expected ${0} not to always be called with arguments${1}${2}";
468```
469
470<dl>
471 <dt>`${0}`:</dt>
472 <dd>The spy</dd>
473 <dt>`${1}`:</dt>
474 <dd>The expected arguments</dd>
475 <dt>`${2}`:</dt>
476 <dd>String representation of all calls</dd>
477</dl>
478
479### `alwaysCalledWithNew()`
480
481```js
482assert.alwaysCalledWithNew(spy)
483```
484
485Passes when the `spy` has was always called with `new`
486
487```js
488var spy = this.spy();
489
490assert.alwaysCalledWithNew(spy); // Fails
491
492new spy();
493assert.alwaysCalledWithNew(spy); // Passes
494
495spy();
496assert.alwaysCalledWithNew(spy); // Fails
497```
498
499#### Messages
500
501```js
502assert.calledWithNew.message = "Expected ${spyObj} to always be called with 'new'";
503refute.calledWithNew.message = "Expected ${spyObj} to not always be called with 'new'";
504```
505
506<dl>
507 <dt>`${spyObj}`:</dt>
508 <dd>The spy</dd>
509</dl>
510
511
512### `calledOnceWith()`
513
514```js
515assert.calledOnceWith(spy, arg1, arg2, ...)
516```
517
518Passes if the `spy` was called exactly once and with the specified arguments. Other arguments may have been passed after the specified ones.
519
520```js
521var spy = this.spy();
522var arr = [1, 2, 3];
523spy(12);
524
525assert.calledOnceWith(spy, 12); // Passes
526assert.calledOnceWith(spy, 42); // Fails
527
528spy(42, 13);
529assert.calledOnceWith(spy, 42, 13); // Fails
530```
531
532#### Messages
533
534```js
535assert.calledOnceWith.message = "Expected ${0} to be called once with arguments ${1}${2}";
536refute.calledOnceWith.message = "Expected ${0} not to be called once with arguments ${1}${2}";
537```
538
539<dl>
540 <dt>`${0}`:</dt>
541 <dd>The spy</dd>
542 <dt>`${1}`:</dt>
543 <dd>The expected arguments</dd>
544 <dt>`${2}`:</dt>
545 <dd>String representation of all calls</dd>
546</dl>
547
548### `calledWithExactly()`
549
550```js
551assert.calledWithExactly(spy, arg1, arg2, ...)
552```
553
554Passes if the `spy` was called at least once with exactly the arguments specified.
555
556```js
557var spy = this.spy();
558var arr = [1, 2, 3];
559spy("Hey", arr, 12);
560spy("Hey", arr, 13);
561
562assert.calledWithExactly(spy, "Hey", arr, 12); // Passes
563assert.calledWithExactly(spy, "Hey", arr, 13); // Passes
564assert.calledWithExactly(spy, "Hey", arr); // Fails
565assert.calledWithExactly(spy, "Hey"); // Fails
566```
567
568#### Messages
569
570```js
571assert.calledWithExactly.message = "Expected ${0} to be called with exact arguments ${1}${2}";
572refute.calledWithExactly.message = "Expected ${0} not to be called with exact arguments${1}${2}";
573```
574
575<dl>
576 <dt>`${0}`:</dt>
577 <dd>The spy</dd>
578 <dt>`${1}`:</dt>
579 <dd>The expected arguments</dd>
580 <dt>`${2}`:</dt>
581 <dd>String representation of all calls</dd>
582</dl>
583
584### `alwaysCalledWithExactly()`
585
586```js
587assert.alwaysCalledWithExactly(spy, arg1, arg2, ...)
588```
589
590Passes if the `spy` was always called with exactly the arguments specified.
591
592```js
593var spy = this.spy();
594var arr = [1, 2, 3];
595spy("Hey", arr, 12);
596
597assert.alwaysCalledWithExactly(spy, "Hey", arr, 12); // Passes
598assert.alwaysCalledWithExactly(spy, "Hey", arr); // Fails
599assert.alwaysCalledWithExactly(spy, "Hey"); // Fails
600
601spy("Hey", arr, 13);
602assert.alwaysCalledWithExactly(spy, "Hey", arr, 12); // Fails
603```
604
605#### Messages
606
607```js
608assert.alwaysCalledWithExactly.message = "Expected ${0} to always be called with exact arguments ${1}${2}";
609refute.alwaysCalledWithExactly.message = "Expected ${0} not to always be called with exact arguments${1}${2}";
610```
611
612<dl>
613 <dt>`${0}`:</dt>
614 <dd>The spy</dd>
615 <dt>`${1}`:</dt>
616 <dd>The expected arguments</dd>
617 <dt>`${2}`:</dt>
618 <dd>String representation of all calls</dd>
619</dl>
620
621
622### `threw()`
623
624```js
625assert.threw(spy[, exception])
626```
627
628Passes if the `spy` threw at least once the specified `exception`. The `exception` can be a string denoting its type, or an actual object. If `exception` is not specified, the assertion passes if the `spy` ever threw any exception.
629
630```js
631var exception1 = new TypeError();
632var exception2 = new TypeError();
633var exception3 = new TypeError();
634var spy = this.spy(function(exception) {
635 throw exception;
636});
637
638function callAndCatchException(spy, exception) {
639 try {
640 spy(exception);
641 } catch(e) {
642 }
643}
644
645callAndCatchException(spy, exception1);
646callAndCatchException(spy, exception2);
647
648assert.threw(spy); // Passes
649assert.threw(spy, “TypeError”); // Passes
650assert.threw(spy, exception1); // Passes
651assert.threw(spy, exception2); // Passes
652assert.threw(spy, exception3); // Fails
653
654callAndCatchException(spy, exception3); assert.threw(spy, exception3); // Passes
655```
656
657#### Messages
658
659```js
660assert.threw.message = "Expected ${0} to throw an exception${1}";
661refute.threw.message = "Expected ${0} not to throw an exception${1}";
662```
663
664<dl>
665 <dt>`${0}`:</dt>
666 <dd>The spy</dd>
667 <dt>`${1}`:</dt>
668 <dd>The expected exception</dd>
669</dl>
670
671### `alwaysThrew()`
672
673```js
674assert.alwaysThrew(spy[, exception])
675```
676
677Passes if the `spy` always threw the specified `exception`. The `exception` can be a string denoting its type, or an actual object. If `exception` is not specified, the assertion passes if the `spy` ever threw any exception.
678
679```js
680var exception1 = new TypeError();
681var exception2 = new TypeError();
682var spy = this.spy(function(exception) {
683 throw exception;
684});
685
686function callAndCatchException(spy, exception) {
687 try {
688 spy(exception);
689 } catch(e) {
690
691 }
692}
693
694callAndCatchException(spy, exception1);
695assert.alwaysThrew(spy); // Passes
696assert.alwaysThrew(spy, “TypeError”); // Passes
697assert.alwaysThrew(spy, exception1); // Passes
698
699callAndCatchException(spy, exception2);
700assert.alwaysThrew(spy); // Passes
701assert.alwaysThrew(spy, “TypeError”); // Passes
702assert.alwaysThrew(spy, exception1); // Fails
703```
704
705#### Messages
706
707```js
708assert.alwaysThrew.message = "Expected ${0} to always throw an exception${1}";
709refute.alwaysThrew.message = "Expected ${0} not to always throw an exception${1}";
710```
711
712<dl>
713 <dt>`${0}`:</dt>
714 <dd>The spy</dd>
715 <dt>`${1}`:</dt>
716 <dd>The expected exception</dd>
717</dl>