Skip to content

Commit f77edbd

Browse files
committed
Add release documentation for v8.0.3
1 parent 8b55dea commit f77edbd

24 files changed

+3372
-9
lines changed

docs/_releases/latest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: page
33
title: API documentation - Sinon.JS
44
skip_ad: true
5-
release_id: v8.0.2
5+
release_id: v8.0.3
66
---
77

88
# {{page.title}} - `{{page.release_id}}`

docs/_releases/latest/fake-timers.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ As above, but allows further configuration options, some of which are:
6363
- `config.toFake` - *String[ ]* - an array with explicit function names to fake. By default lolex will automatically fake all methods *except* `process.nextTick`. You could, however, still fake `nextTick` by providing it explicitly
6464
- `config.shouldAdvanceTime` - *Boolean* - tells lolex to increment mocked time automatically based on the real system time shift (default: false)
6565

66-
Please visit the `lolex.install` [documentation](https://github.com/sinonjs/lolex#var-clock--lolexinstallconfig) for the full feature set.
66+
Please refer to the `lolex.install` [documentation](https://github.com/sinonjs/lolex#var-clock--lolexinstallconfig) for the full set of features available and more elaborate explanations.
67+
68+
*Since `sinon@3.0.0`*
69+
70+
`var clock = sinon.useFakeTimers([now, ]prop1, prop2, ...)` is no longer supported. To define which methods to fake, please use `config.toFake`.
6771

6872
**Important note:** when faking `nextTick`, normal calls to `process.nextTick()` would not execute automatically as they would during normal event-loop phases. You would have to call either `clock.next()`, `clock.tick()`, `clock.runAll()` or `clock.runToLast()` (see example below). Please refer to the [lolex](https://github.com/sinonjs/lolex) documentation for more information.
6973

@@ -108,31 +112,62 @@ setTimeout(function () {
108112
}, 35);
109113
```
110114

111-
Please refer to the `lolex.install` [documentation](https://github.com/sinonjs/lolex#var-clock--lolexinstallconfig) for the full set of features available and more elaborate explanations.
115+
Using fake timers with `async` / `await`:
112116

113-
*Since `sinon@3.0.0`*
117+
```
118+
async function asyncFn() {
114119
115-
`var clock = sinon.useFakeTimers([now, ]prop1, prop2, ...)` is no longer supported. To define which methods to fake, please use `config.toFake`.
120+
await wait(100);
121+
122+
console.log('resolved 1', Date.now());
123+
124+
await wait(10);
125+
126+
console.log('resolved 2', Date.now());
127+
}
128+
129+
async function test() {
130+
131+
const clock = sinon.useFakeTimers();
132+
133+
setTimeout(() => console.log('timeout', Date.now()), 200);
134+
135+
asyncFn(); // NOTE: no `await` here - it would hang, as the clock is stopped
136+
137+
await clock.tickAsync(200);
138+
}
139+
140+
// test() prints:
141+
// - resolved 1 100
142+
// - resolved 2 110
143+
// - timeout 200
144+
```
116145

146+
Note that in the above example, the synchronous `clock.tick(200)` would only print `timeout 200` and `resolved 1 200`.
117147

118-
#### `clock.tick(time);`
148+
149+
#### `clock.tick(time);` / `await clock.tickAsync(time)`
119150

120151
Tick the clock ahead `time` milliseconds.
121152

122153
Causes all timers scheduled within the affected time range to be called. `time` may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
123154

124-
time may be negative, which causes the clock to change but won't fire any callbacks.
155+
The `tickAsync()` will also break the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.
125156

126-
#### `clock.next();`
157+
#### `clock.next();` / `await clock.nextAsync()`
127158

128159
Advances the clock to the the moment of the first scheduled timer, firing it.
129160

130-
#### `clock.runAll();`
161+
The `nextAsync()` will also break the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.
162+
163+
#### `clock.runAll();` / `await clock.runAllAsync()`
131164

132165
This runs all pending timers until there are none remaining. If new timers are added while it is executing they will be run as well.
133166

134167
This makes it easier to run asynchronous tests to completion without worrying about the number of timers they use, or the delays in those timers.
135168

169+
The `runAllAsync()` will also break the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.
170+
136171
#### `clock.restore();`
137172

138173
Restore the faked methods.

docs/_releases/v8.0.3.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
layout: page
3+
title: API documentation - Sinon.JS
4+
skip_ad: true
5+
release_id: v8.0.3
6+
---
7+
8+
# {{page.title}} - `{{page.release_id}}`
9+
10+
This page contains the entire Sinon.JS API documentation along with brief introductions to the concepts Sinon implements.
11+
12+
* [General setup](./general-setup)
13+
* [Fakes](./fakes)
14+
* [Spies](./spies)
15+
* [Stubs](./stubs)
16+
* [Mocks](./mocks)
17+
* [Spy calls](./spy-call)
18+
* [Fake timers](./fake-timers)
19+
* [Fake <code>XHR</code> and server](./fake-xhr-and-server)
20+
* [JSON-P](./json-p)
21+
* [Assertions](./assertions)
22+
* [Matchers](./matchers)
23+
* [Sandboxes](./sandbox)
24+
* [Utils](./utils)
25+
26+
{% include docs/migration-guides.md %}
27+
28+
### Compatibility
29+
30+
### ES5.1
31+
32+
Sinon `{{page.release_id}}` is written as [ES5.1][ES5] and requires no transpiler or polyfills to run in the runtimes listed below.
33+
34+
### Supported runtimes
35+
36+
`{{page.release_id}}` has been verified in these runtimes:
37+
38+
* Firefox 45
39+
* Chrome 48
40+
* Internet Explorer 11
41+
* Edge 14
42+
* Safari 9
43+
* [Node.js LTS versions](https://github.com/nodejs/Release)
44+
45+
There should not be any issues with using Sinon `{{page.release_id}}` in newer versions of the same runtimes.
46+
47+
If you need to support very old runtimes that have incomplete support for [ES5.1][ES5] you might get away with using loading [`es5-shim`][es5-shim] in your test environment.
48+
49+
{% include docs/contribute.md %}
50+
51+
[ES5]: http://www.ecma-international.org/ecma-262/5.1/
52+
[es5-shim]: https://github.com/es-shims/es5-shim
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
---
2+
layout: page
3+
title: Assertions - Sinon.JS
4+
breadcrumb: assertions
5+
---
6+
7+
Sinon.JS ships with a set of assertions that mirror most behavior verification methods and properties on spies and stubs. The advantage of using the assertions is that failed expectations on stubs and spies can be expressed directly as assertion failures with detailed and helpful error messages.
8+
9+
To make sure assertions integrate nicely with your test framework, you should customize either `sinon.assert.fail` or `sinon.assert.failException` and look into `sinon.assert.expose` and `sinon.assert.pass`.
10+
11+
The assertions can be used with either spies or stubs.
12+
13+
```javascript
14+
"test should call subscribers with message as first argument" : function () {
15+
var message = "an example message";
16+
var spy = sinon.spy();
17+
18+
PubSub.subscribe(message, spy);
19+
PubSub.publishSync(message, "some payload");
20+
21+
sinon.assert.calledOnce(spy);
22+
sinon.assert.calledWith(spy, message);
23+
}
24+
```
25+
26+
## Assertions API
27+
28+
#### `sinon.assert.fail(message)`
29+
30+
Every assertion fails by calling this method.
31+
32+
By default it throws an error of type `sinon.assert.failException`.
33+
34+
If the test framework looks for assertion errors by checking for a specific exception, you can override the kind of exception thrown. If that does not fit with your testing framework of choice, override the `fail` method to do the right thing.
35+
36+
37+
#### `sinon.assert.failException;`
38+
39+
Defaults to `AssertError`.
40+
41+
42+
#### `sinon.assert.pass(assertion);`
43+
44+
Called every time `assertion` passes.
45+
46+
Default implementation does nothing.
47+
48+
49+
#### `sinon.assert.notCalled(spy);`
50+
51+
Passes if `spy` was never called
52+
53+
#### `sinon.assert.called(spy);`
54+
55+
Passes if `spy` was called at least once.
56+
57+
58+
#### `sinon.assert.calledOnce(spy);`
59+
60+
Passes if `spy` was called once and only once.
61+
62+
63+
#### `sinon.assert.calledTwice(spy);`
64+
65+
Passes if `spy` was called exactly twice.
66+
67+
68+
#### `sinon.assert.calledThrice(spy)`
69+
70+
Passes if `spy` was called exactly three times.
71+
72+
73+
#### `sinon.assert.callCount(spy, num)`
74+
Passes if `spy` was called exactly `num` times.
75+
76+
77+
#### `sinon.assert.callOrder(spy1, spy2, ...)`
78+
Passes if provided spies were called in the specified order.
79+
80+
81+
#### `sinon.assert.calledOn(spyOrSpyCall, obj)`
82+
83+
Passes if `spy` was ever called with `obj` as its `this` value.
84+
85+
It's possible to assert on a dedicated spy call: `sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);`.
86+
87+
88+
#### `sinon.assert.alwaysCalledOn(spy, obj)`
89+
90+
Passes if `spy` was always called with `obj` as its `this` value.
91+
92+
93+
#### `sinon.assert.calledWith(spyOrSpyCall, arg1, arg2, ...);`
94+
95+
Passes if `spy` was called with the provided arguments.
96+
97+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);`.
98+
99+
100+
#### `sinon.assert.alwaysCalledWith(spy, arg1, arg2, ...);`
101+
102+
Passes if `spy` was always called with the provided arguments.
103+
104+
105+
#### `sinon.assert.neverCalledWith(spy, arg1, arg2, ...);`
106+
107+
Passes if `spy` was never called with the provided arguments.
108+
109+
110+
#### `sinon.assert.calledWithExactly(spyOrSpyCall, arg1, arg2, ...);`
111+
112+
Passes if `spy` was called with the provided arguments and no others.
113+
114+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);`.
115+
116+
117+
#### `sinon.assert.alwaysCalledWithExactly(spy, arg1, arg2, ...);`
118+
119+
Passes if `spy` was always called with the provided arguments and no others.
120+
121+
122+
#### `sinon.assert.calledWithMatch(spyOrSpyCall, arg1, arg2, ...)`
123+
124+
Passes if `spy` was called with matching arguments.
125+
126+
This behaves the same way as `sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
127+
128+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);`.
129+
130+
131+
#### `sinon.assert.alwaysCalledWithMatch(spy, arg1, arg2, ...)`
132+
133+
Passes if `spy` was always called with matching arguments.
134+
135+
This behaves the same way as `sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
136+
137+
138+
#### `sinon.assert.calledWithNew(spyOrSpyCall)`
139+
140+
Passes if `spy` was called with the `new` operator.
141+
142+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);`.
143+
144+
145+
#### `sinon.assert.neverCalledWithMatch(spy, arg1, arg2, ...)`
146+
147+
Passes if `spy` was never called with matching arguments.
148+
149+
This behaves the same way as `sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
150+
151+
152+
#### `sinon.assert.threw(spyOrSpyCall, exception);`
153+
154+
Passes if `spy` threw the given exception.
155+
156+
The exception can be a `String` denoting its type, or an actual object.
157+
158+
If only one argument is provided, the assertion passes if `spy` ever threw any exception.
159+
160+
It's possible to assert on a dedicated spy call: `sinon.assert.threw(spy.thirdCall, exception);`.
161+
162+
163+
#### `sinon.assert.alwaysThrew(spy, exception);`
164+
165+
Like above, only required for all calls to the spy.
166+
167+
#### `sinon.assert.match(actual, expectation);`
168+
169+
Uses [`sinon.match`](../matchers) to test if the arguments can be considered a match.
170+
171+
```javascript
172+
var sinon = require('sinon');
173+
174+
describe('example', function(){
175+
it('should match on `x` property, and ignore `y` property', function() {
176+
var expected = {x: 1},
177+
actual = {x: 1, y: 2};
178+
179+
sinon.assert.match(actual, expected);
180+
});
181+
});
182+
```
183+
184+
#### `sinon.assert.expose(object, options);`
185+
186+
Exposes assertions into another object, to better integrate with the test framework. For instance, JsTestDriver uses global assertions, and to make Sinon.JS assertions appear alongside them, you can do.
187+
188+
```javascript
189+
sinon.assert.expose(this);
190+
```
191+
192+
This will give you `assertCalled(spy)`,`assertCallOrder(spy1, spy2, ...)` and so on.
193+
194+
The method accepts an optional options object with two options.
195+
196+
<dl>
197+
<dt>prefix</dt>
198+
<dd>is a prefix to give assertions. By default it is "assert", so <code>sinon.assert.called</code> becomes <code>target.assertCalled</code>. By passing a blank string, the exposed method will be <code>target.called</code>.</dd>
199+
200+
<dt>includeFail</dt>
201+
<dd><code>true</code> by default, copies over the <code>fail</code> and <code>failException</code> properties</dd>
202+
</dl>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var PubSub = require("pubsub-js");
4+
var referee = require("@sinonjs/referee");
5+
var assertTrue = referee.assert;
6+
7+
describe('PubSub', function() {
8+
it("should call subscribers on publish", function () {
9+
var callback = sinon.spy();
10+
11+
PubSub.subscribe("message", callback);
12+
PubSub.publishSync("message");
13+
14+
assertTrue(callback.called);
15+
})
16+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var referee = require("@sinonjs/referee");
4+
var assert = referee.assert;
5+
var jsdom = require('jsdom');
6+
var JSDOM = jsdom.JSDOM;
7+
var window = (new JSDOM()).window;
8+
var document = (new JSDOM('')).window;
9+
var jQuery = require('jquery')(window);
10+
global.document = document;
11+
12+
describe('Wrap all object method', function() {
13+
var sandbox = sinon.createSandbox();
14+
15+
beforeEach(function() {
16+
sandbox.spy(jQuery);
17+
});
18+
19+
afterEach(function() {
20+
sandbox.restore();
21+
});
22+
23+
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () {
24+
var url = "https://jsonplaceholder.typicode.com/todos/1";
25+
jQuery.getJSON(url);
26+
27+
assert(jQuery.ajax.calledOnce);
28+
assert.equals(url, jQuery.ajax.getCall(0).args[0].url);
29+
assert.equals("json", jQuery.ajax.getCall(0).args[0].dataType);
30+
})
31+
});

0 commit comments

Comments
 (0)