Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add release documentation for v6.0.1
  • Loading branch information
mroderick committed Jun 24, 2018
1 parent feee43d commit 8f8b2d9
Show file tree
Hide file tree
Showing 13 changed files with 2,906 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/_releases/v6.0.1.md
@@ -0,0 +1,51 @@
---
layout: page
title: API documentation - Sinon.JS
release_id: v6.0.1
---

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

This page contains the entire Sinon.JS API documentation along with brief introductions to the concepts Sinon implements.

* [Fakes](./fakes)
* [Spies](./spies)
* [Stubs](./stubs)
* [Mocks](./mocks)
* [Spy calls](./spy-call)
* [Fake timers](./fake-timers)
* [Fake <code>XHR</code> and server](./fake-xhr-and-server)
* [JSON-P](./json-p)
* [Assertions](./assertions)
* [Matchers](./matchers)
* [Sandboxes](./sandbox)
* [Utils](./utils)

{% include docs/migration-guides.md %}

### Compatibility

### ES5.1

Sinon `{{page.release_id}}` is written as [ES5.1][ES5] and requires no transpiler or polyfills to run in the runtimes listed below.

### Supported runtimes

`{{page.release_id}}` has been verified in these runtimes:

* Firefox 45
* Chrome 48
* Internet Explorer 11
* Edge 14
* Safari 9
* [Node.js LTS versions](https://github.com/nodejs/Release)

There should not be any issues with using Sinon `{{page.release_id}}` in newer versions of the same runtimes.

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. If that fails, we recommend [getting a legacy releases of Sinon][legacy-site].

{% include docs/contribute.md %}

[ES5]: http://www.ecma-international.org/ecma-262/5.1/
[es5-shim]: https://github.com/es-shims/es5-shim
[legacy-site]: http://legacy.sinonjs.org
202 changes: 202 additions & 0 deletions docs/_releases/v6.0.1/assertions.md
@@ -0,0 +1,202 @@
---
layout: page
title: Assertions - Sinon.JS
breadcrumb: assertions
---

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.

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`.

The assertions can be used with either spies or stubs.

```javascript
"test should call subscribers with message as first argument" : function () {
var message = "an example message";
var spy = sinon.spy();

PubSub.subscribe(message, spy);
PubSub.publishSync(message, "some payload");

sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, message);
}
```

## Assertions API

#### `sinon.assert.fail(message)`

Every assertion fails by calling this method.

By default it throws an error of type `sinon.assert.failException`.

If the test framework looks for assertion errors by checking for a specific exception, you can simply 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.


#### `sinon.assert.failException;`

Defaults to `AssertError`.


#### `sinon.assert.pass(assertion);`

Called every time `assertion` passes.

Default implementation does nothing.


#### `sinon.assert.notCalled(spy);`

Passes if `spy` was never called

#### `sinon.assert.called(spy);`

Passes if `spy` was called at least once.


#### `sinon.assert.calledOnce(spy);`

Passes if `spy` was called once and only once.


#### `sinon.assert.calledTwice(spy);`

Passes if `spy` was called exactly twice.


#### `sinon.assert.calledThrice(spy)`

Passes if `spy` was called exactly three times.


#### `sinon.assert.callCount(spy, num)`
Passes if `spy` was called exactly `num` times.


#### `sinon.assert.callOrder(spy1, spy2, ...)`
Passes if provided spies were called in the specified order.


#### `sinon.assert.calledOn(spyOrSpyCall, obj)`

Passes if `spy` was ever called with `obj` as its `this` value.

It's possible to assert on a dedicated spy call: `sinon.assert.calledOn(spy.firstCall, arg1, arg2, ...);`.


#### `sinon.assert.alwaysCalledOn(spy, obj)`

Passes if `spy` was always called with `obj` as its `this` value.


#### `sinon.assert.calledWith(spyOrSpyCall, arg1, arg2, ...);`

Passes if `spy` was called with the provided arguments.

It's possible to assert on a dedicated spy call: `sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);`.


#### `sinon.assert.alwaysCalledWith(spy, arg1, arg2, ...);`

Passes if `spy` was always called with the provided arguments.


#### `sinon.assert.neverCalledWith(spy, arg1, arg2, ...);`

Passes if `spy` was never called with the provided arguments.


#### `sinon.assert.calledWithExactly(spyOrSpyCall, arg1, arg2, ...);`

Passes if `spy` was called with the provided arguments and no others.

It's possible to assert on a dedicated spy call: `sinon.assert.calledWithExactly(spy.getCall(1), arg1, arg2, ...);`.


#### `sinon.assert.alwaysCalledWithExactly(spy, arg1, arg2, ...);`

Passes if `spy` was always called with the provided arguments and no others.


#### `sinon.assert.calledWithMatch(spyOrSpyCall, arg1, arg2, ...)`

Passes if `spy` was called with matching arguments.

This behaves the same way as `sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.

It's possible to assert on a dedicated spy call: `sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);`.


#### `sinon.assert.alwaysCalledWithMatch(spy, arg1, arg2, ...)`

Passes if `spy` was always called with matching arguments.

This behaves the same way as `sinon.assert.alwaysCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.


#### `sinon.assert.calledWithNew(spyOrSpyCall)`

Passes if `spy` was called with the `new` operator.

It's possible to assert on a dedicated spy call: `sinon.assert.calledWithNew(spy.secondCall, arg1, arg2, ...);`.


#### `sinon.assert.neverCalledWithMatch(spy, arg1, arg2, ...)`

Passes if `spy` was never called with matching arguments.

This behaves the same way as `sinon.assert.neverCalledWith(spy, sinon.match(arg1), sinon.match(arg2), ...)`.


#### `sinon.assert.threw(spyOrSpyCall, exception);`

Passes if `spy` threw the given exception.

The exception can be a `String` denoting its type, or an actual object.

If only one argument is provided, the assertion passes if `spy` ever threw any exception.

It's possible to assert on a dedicated spy call: `sinon.assert.threw(spy.thirdCall, exception);`.


#### `sinon.assert.alwaysThrew(spy, exception);`

Like above, only required for all calls to the spy.

#### `sinon.assert.match(actual, expectation);`

Uses [`sinon.match`](../matchers) to test if the arguments can be considered a match.

```javascript
var sinon = require('sinon');

describe('example', function(){
it('should match on `x` property, and ignore `y` property', function() {
var expected = {x: 1},
actual = {x: 1, y: 2};

sinon.assert.match(actual, expected);
});
});
```

#### `sinon.assert.expose(object, options);`

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.

```javascript
sinon.assert.expose(this);
```

This will give you `assertCalled(spy)`,`assertCallOrder(spy1, spy2, ...)` and so on.

The method accepts an optional options object with two options.

<dl>
<dt>prefix</dt>
<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>

<dt>includeFail</dt>
<dd><code>true</code> by default, copies over the <code>fail</code> and <code>failException</code> properties</dd>
</dl>
140 changes: 140 additions & 0 deletions docs/_releases/v6.0.1/fake-timers.md
@@ -0,0 +1,140 @@
---
layout: page
title: Fake timers - Sinon.JS
breadcrumb: fake timers
---

Fake timers are synchronous implementations of `setTimeout` and friends that
Sinon.JS can overwrite the global functions with to allow you to more easily
test code using them.

Fake timers provide a `clock` object to pass time, which can also be used to control `Date` objects created through either `new Date();`
or `Date.now();` (if supported by the browser).

For standalone usage of fake timers it is recommended to use [lolex](https://github.com/sinonjs/lolex) package instead. It provides the same
set of features (Sinon uses it under the hood) and was previously extracted from Sinon.JS.

```javascript
{
setUp: function () {
this.clock = sinon.useFakeTimers();
},

tearDown: function () {
this.clock.restore();
},

"test should animate element over 500ms" : function(){
var el = jQuery("<div></div>");
el.appendTo(document.body);

el.animate({ height: "200px", width: "200px" });
this.clock.tick(510);

assertEquals("200px", el.css("height"));
assertEquals("200px", el.css("width"));
}
}
```

## Fake timers API


#### `var clock = sinon.useFakeTimers();`

Causes Sinon to replace the global `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval`, `setImmediate`, `clearImmediate`, `process.hrtime`, `performance.now`(when available) and `Date` with a custom implementation which is bound to the returned `clock` object.

Starts the clock at the UNIX epoch (timestamp of `0`).


#### `var clock = sinon.useFakeTimers(now);`

As above, but rather than starting the clock with a timestamp of 0, start at the provided timestamp `now`.

*Since `sinon@2.0.0`*

You can also pass in a Date object, and its `getTime()` will be used for the starting timestamp.

#### `var clock = sinon.useFakeTimers(config);`

As above, but allows further configuration options, some of which are:

- `config.now` - *Number/Date* - installs lolex with the specified unix epoch (default: 0)
- `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
- `config.shouldAdvanceTime` - *Boolean* - tells lolex to increment mocked time automatically based on the real system time shift (default: false)

Please visit the `lolex.install` [documentation](https://github.com/sinonjs/lolex#var-clock--lolexinstallconfig) for the full feature set.

**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.

#### Examples

Installs fake timers at January 1st 2017 and fakes `setTimeout` and `process.nextTick` only:

```javascript
var clock = sinon.useFakeTimers({
now: 1483228800000,
toFake: ["setTimeout", "nextTick"]
});

var called = false;

process.nextTick(function () {
called = true;
});

clock.runAll(); //forces nextTick calls to flush synchronously
assert(called); //true
```

Install at the same date, advancing the fake time automatically (default is every `20ms`), causing timers to be fired automatically without the need to `tick()` the clock:

```js
var clock = sinon.useFakeTimers({
now: 1483228800000,
shouldAdvanceTime: true
});

setImmediate(function () {
console.log('tick'); //will print after 20ms
});

setTimeout(function () {
console.log('tock'); //will print after 20ms
}, 15);

setTimeout(function () {
console.log('tack'); //will print after 40ms
}, 35);
```

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.

*Since `sinon@3.0.0`*

`var clock = sinon.useFakeTimers([now, ]prop1, prop2, ...)` is no longer supported. To define which methods to fake, please use `config.toFake`.


#### `clock.tick(time);`

Tick the clock ahead `time` milliseconds.

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.

time may be negative, which causes the clock to change but won't fire any callbacks.

#### `clock.next();`

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

#### `clock.runAll();`

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.

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.

#### `clock.restore();`

Restore the faked methods.

Call in e.g. `tearDown`.

0 comments on commit 8f8b2d9

Please sign in to comment.