Skip to content

Commit fedeebf

Browse files
committed
Add release documentation for v12.0.0
1 parent 0cf9235 commit fedeebf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3644
-4
lines changed

docs/_releases/latest.md

+1-1
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: v11.1.2
5+
release_id: v12.0.0
66
---
77

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

docs/_releases/latest/fake-xhr-and-server.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Causes all queued asynchronous requests to receive a response.
284284

285285
If none of the responses added through `respondWith()` match, the default response is `[404, {}, ""]`.
286286

287-
Synchronous requests are responded to immediately so make sure to call `respondWith()` to configure the server response before calling `respond()`. If not, you will recieve the default `404 NOT FOUND` response.
287+
Synchronous requests are responded to immediately so make sure to call `respondWith()` to configure the server response before calling `respond()`. If not, you will receive the default `404 NOT FOUND` response.
288288

289289
If called with arguments, `respondWith()` will be called with those arguments before responding to requests.
290290

docs/_releases/latest/spy-call.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ breadcrumb: spy-call
66

77
## Spy call
88

9-
A spy call is an object representation of an invididual call to a _spied_ function, which could be a [fake](../fakes), [spy](../spies), [stub](../stubs) or [mock method](../mocks).
9+
A spy call is an object representation of an individual call to a _spied_ function, which could be a [fake](../fakes), [spy](../spies), [stub](../stubs) or [mock method](../mocks).
1010

1111
### `var spyCall = spy.getCall(n)`
1212

docs/_releases/latest/utils.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Sinon.JS has a few utilities used internally in `lib/sinon.js`. Unless the metho
1010

1111
#### `sinon.createStubInstance(constructor);`
1212

13-
Creates a new object with the given function as the protoype and stubs all implemented functions.
13+
Creates a new object with the given function as the prototype and stubs all implemented functions.
1414

1515
```javascript
1616
class Container {

docs/_releases/v12.0.0.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: page
3+
title: API documentation - Sinon.JS
4+
skip_ad: true
5+
release_id: v12.0.0
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+
- [Promises](./promises)
19+
- [Fake timers](./fake-timers)
20+
- [Fake <code>XHR</code> and server](./fake-xhr-and-server)
21+
- [JSON-P](./json-p)
22+
- [Assertions](./assertions)
23+
- [Matchers](./matchers)
24+
- [Sandboxes](./sandbox)
25+
- [Utils](./utils)
26+
27+
{% include docs/migration-guides.md %}
28+
29+
## Compatibility and supported runtimes
30+
31+
As of Sinon 10 we stopped maintaining compatibility with legacy browsers. Instead, we focus on compatibility with evergreen browsers, [Node.js LTS versions](https://github.com/nodejs/Release) and recent Safari versions.
32+
The most up-to-date reference on which runtimes and browsers we support can be found by looking at our [compatibility docs][compat-doc].
33+
34+
If you need to support old runtimes you can try [Sinon 9][compat-doc-v9].
35+
36+
{% include docs/contribute.md %}
37+
38+
[compat-doc]: https://github.com/sinonjs/sinon/COMPATIBILITY.md
39+
[compat-doc-v9]: https://github.com/sinonjs/sinon/blob/v9.2.4/COMPATIBILITY.md

docs/_releases/v12.0.0/assertions.md

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
#### `sinon.assert.failException;`
37+
38+
Defaults to `AssertError`.
39+
40+
#### `sinon.assert.pass(assertion);`
41+
42+
Called every time `assertion` passes.
43+
44+
Default implementation does nothing.
45+
46+
#### `sinon.assert.notCalled(spy);`
47+
48+
Passes if `spy` was never called
49+
50+
#### `sinon.assert.called(spy);`
51+
52+
Passes if `spy` was called at least once.
53+
54+
#### `sinon.assert.calledOnce(spy);`
55+
56+
Passes if `spy` was called once and only once.
57+
58+
#### `sinon.assert.calledTwice(spy);`
59+
60+
Passes if `spy` was called exactly twice.
61+
62+
#### `sinon.assert.calledThrice(spy)`
63+
64+
Passes if `spy` was called exactly three times.
65+
66+
#### `sinon.assert.callCount(spy, num)`
67+
68+
Passes if `spy` was called exactly `num` times.
69+
70+
#### `sinon.assert.callOrder(spy1, spy2, ...)`
71+
72+
Passes if provided spies were called in the specified order.
73+
74+
#### `sinon.assert.calledOn(spyOrSpyCall, obj)`
75+
76+
Passes if `spy` was ever called with `obj` as its `this` value.
77+
78+
It's possible to assert on a dedicated spy call: `sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);`.
79+
80+
#### `sinon.assert.alwaysCalledOn(spy, obj)`
81+
82+
Passes if `spy` was always called with `obj` as its `this` value.
83+
84+
#### `sinon.assert.calledWith(spyOrSpyCall, arg1, arg2, ...);`
85+
86+
Passes if `spy` was called with the provided arguments.
87+
88+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);`.
89+
90+
#### `sinon.assert.alwaysCalledWith(spy, arg1, arg2, ...);`
91+
92+
Passes if `spy` was always called with the provided arguments.
93+
94+
#### `sinon.assert.neverCalledWith(spy, arg1, arg2, ...);`
95+
96+
Passes if `spy` was never called with the provided arguments.
97+
98+
#### `sinon.assert.calledWithExactly(spyOrSpyCall, arg1, arg2, ...);`
99+
100+
Passes if `spy` was called with the provided arguments and no others.
101+
102+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);`.
103+
104+
#### `sinon.assert.calledOnceWithExactly(spyOrSpyCall, arg1, arg2, ...);`
105+
106+
Passes if `spy` was called once and only once with the provided arguments and no others.
107+
108+
It's possible to assert on a dedicated spy call: `sinon.assert.calledOnceWithExactly(spy.getCall(1), arg1, arg2, ...);`.
109+
110+
#### `sinon.assert.alwaysCalledWithExactly(spy, arg1, arg2, ...);`
111+
112+
Passes if `spy` was always called with the provided arguments and no others.
113+
114+
#### `sinon.assert.calledWithMatch(spyOrSpyCall, arg1, arg2, ...)`
115+
116+
Passes if `spy` was called with matching arguments.
117+
118+
This behaves the same way as `sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
119+
120+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);`.
121+
122+
#### `sinon.assert.alwaysCalledWithMatch(spy, arg1, arg2, ...)`
123+
124+
Passes if `spy` was always called with matching arguments.
125+
126+
This behaves the same way as `sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
127+
128+
#### `sinon.assert.calledWithNew(spyOrSpyCall)`
129+
130+
Passes if `spy` was called with the `new` operator.
131+
132+
It's possible to assert on a dedicated spy call: `sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);`.
133+
134+
#### `sinon.assert.neverCalledWithMatch(spy, arg1, arg2, ...)`
135+
136+
Passes if `spy` was never called with matching arguments.
137+
138+
This behaves the same way as `sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.
139+
140+
#### `sinon.assert.threw(spyOrSpyCall, exception);`
141+
142+
Passes if `spy` threw the given exception.
143+
144+
The exception can be a `String` denoting its type, or an actual object.
145+
146+
If only one argument is provided, the assertion passes if `spy` ever threw any exception.
147+
148+
It's possible to assert on a dedicated spy call: `sinon.assert.threw(spy.thirdCall, exception);`.
149+
150+
#### `sinon.assert.alwaysThrew(spy, exception);`
151+
152+
Like above, only required for all calls to the spy.
153+
154+
#### `sinon.assert.match(actual, expectation);`
155+
156+
Uses [`sinon.match`](../matchers) to test if the arguments can be considered a match.
157+
158+
```javascript
159+
var sinon = require("sinon");
160+
161+
describe("example", function () {
162+
it("should match on `x` property, and ignore `y` property", function () {
163+
var expected = { x: 1 },
164+
actual = { x: 1, y: 2 };
165+
166+
sinon.assert.match(actual, expected);
167+
});
168+
});
169+
```
170+
171+
#### `sinon.assert.expose(object, options);`
172+
173+
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.
174+
175+
```javascript
176+
sinon.assert.expose(this);
177+
```
178+
179+
This will give you `assertCalled(spy)`,`assertCallOrder(spy1, spy2, ...)` and so on.
180+
181+
The method accepts an optional options object with two options.
182+
183+
<dl>
184+
<dt>prefix</dt>
185+
<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>
186+
187+
<dt>includeFail</dt>
188+
<dd><code>true</code> by default, copies over the <code>fail</code> and <code>failException</code> properties</dd>
189+
190+
</dl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
env:
2+
es6: true
3+
4+
parserOptions:
5+
ecmaVersion: 2017
6+
7+
extends:
8+
- ../../../../test/.eslintrc.yml
9+
10+
rules:
11+
no-underscore-dangle: off
12+
no-console: off
13+
no-empty-function: off
14+
mocha/no-setup-in-describe: off
15+
no-var: error
16+
prefer-const: error
17+
strict: off # cannot use strict mode in RunKit due to it using `with` tricks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
describe("FakeTest", function () {
7+
it("should be able to be used instead of spies", function () {
8+
const foo = {
9+
bar: () => "baz",
10+
};
11+
// wrap existing method without changing its behaviour
12+
const fake = sinon.replace(foo, "bar", sinon.fake(foo.bar));
13+
14+
assert.equals(fake(), "baz"); // behaviour is the same
15+
assert.equals(fake.callCount, 1); // calling information is saved
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
describe("FakeTest", function () {
7+
it("should have working firstArg", function () {
8+
const f = sinon.fake();
9+
const date1 = new Date();
10+
const date2 = new Date();
11+
12+
f(date1, 1, 2);
13+
f(date2, 1, 2);
14+
15+
assert.isTrue(f.firstArg === date2);
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
describe("FakeTest", function () {
7+
it("should have working lastArg", function () {
8+
const f = sinon.fake();
9+
const date1 = new Date();
10+
const date2 = new Date();
11+
12+
f(1, 2, date1);
13+
f(1, 2, date2);
14+
15+
assert.isTrue(f.lastArg === date2);
16+
// spy call methods:
17+
assert.isTrue(f.getCall(0).lastArg === date1);
18+
assert.isTrue(f.getCall(1).lastArg === date2);
19+
assert.isTrue(f.lastCall.lastArg === date2);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
describe("FakeTest", function () {
7+
it("should be able to be added to the system under test", function () {
8+
const fake = sinon.fake.returns("42");
9+
10+
sinon.replace(console, "log", fake);
11+
12+
assert.equals(console.log("apple pie"), 42);
13+
14+
// restores all replaced properties set by sinon methods (replace, spy, stub)
15+
sinon.restore();
16+
17+
assert.equals(console.log("apple pie"), undefined);
18+
assert.equals(fake.callCount, 1);
19+
});
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
describe("FakeTest", function () {
7+
it("should be able to be used instead of stubs", function () {
8+
const foo = {
9+
bar: () => "baz",
10+
};
11+
// replace method with a fake one
12+
const fake = sinon.replace(
13+
foo,
14+
"bar",
15+
sinon.fake.returns("fake value")
16+
);
17+
18+
assert.equals(fake(), "fake value"); // returns fake value
19+
assert.equals(fake.callCount, 1); // saves calling information
20+
});
21+
});

0 commit comments

Comments
 (0)