Skip to content

Commit

Permalink
Fix fake tests (#2439)
Browse files Browse the repository at this point in the history
* Fix failing test

* Flatten fake tests and restore sort order

* cleanup

* Update docs to reflect new structure

* Fix formatting
  • Loading branch information
fatso83 authored Jan 31, 2022
1 parent 7f16cec commit 209f71f
Show file tree
Hide file tree
Showing 24 changed files with 183 additions and 213 deletions.
6 changes: 0 additions & 6 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ When you're contributing documentation changes for code in `master` branch, then

If you're contributing documentation for an existing release, then your documentation changes should go into the documentation for that release in `_releases/` folder, and possibly several of the following releases also.

### Example: documenting a fixed bug

Let us say that you are documenting a bug in `v1.17.1` that was fixed in `v1.17.4`.

Then we would need to change the documentation for `v1.17.1`, `v1.17.2` and `v1.17.3` to mention the bug and that it was fixed in `v1.17.4`.

## Running the documentation site locally

For casual improvements to the documentation, this shouldn't really be necessary, as all the content documents are plain markdown files.
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ published: false

This folder structure contains the markdown files that becomes the Sinon.JS documentation site published to GitHub Pages. Eventually this will replace the current site at https://sinonjs.org.

See [CONTRIBUTING.md](CONTRIBUTING.md) for details on contributing documentation to Sinon.JS.
See [CONTRIBUTING.md](CONTRIBUTING.md) for details on contributing documentation to Sinon.JS. This file also lists how to run the site locally.

## Documentation release process

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should be able to be used instead of spies", function () {
const foo = {
bar: () => "baz",
};
// wrap existing method without changing its behaviour
const fake = sinon.replace(foo, "bar", sinon.fake(foo.bar));

assert.equals(fake(), "baz"); // behaviour is the same
assert.equals(fake.callCount, 1); // calling information is saved
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should be able to be used instead of stubs", function () {
const foo = {
bar: () => "baz",
};
// replace method with a fake one
const fake = sinon.replace(foo, "bar", sinon.fake.returns("fake value"));

assert.equals(fake(), "fake value"); // returns fake value
assert.equals(fake.callCount, 1); // saves calling information
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should create fake without behaviour", function () {
// create a basic fake, with no behavior
const fake = sinon.fake();

assert.isUndefined(fake()); // by default returns undefined
assert.equals(fake.callCount, 1); // saves call information
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should create fake with custom behaviour", function () {
// create a fake that returns the text "foo"
const fake = sinon.fake.returns("foo");

assert.equals(fake(), "foo");
});
10 changes: 10 additions & 0 deletions docs/release-source/release/examples/fakes-05-returns.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should create a fake that 'returns'", function () {
const fake = sinon.fake.returns("apple pie");

assert.equals(fake(), "apple pie");
});
11 changes: 11 additions & 0 deletions docs/release-source/release/examples/fakes-06-throws.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should create a fake that 'throws'", function () {
const fake = sinon.fake.throws(new Error("not apple pie"));

// Expected to throw an error with message 'not apple pie'
assert.exception(fake, { name: "Error", message: "not apple pie" });
});
23 changes: 23 additions & 0 deletions docs/release-source/release/examples/fakes-07-yields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;
const fs = require("fs");

it("should create a fake that 'yields'", function () {
const fake = sinon.fake.yields(null, "file content");
const anotherFake = sinon.fake();

sinon.replace(fs, "readFile", fake);
fs.readFile("somefile", (err, data) => {
// called with fake values given to yields as arguments
assert.isNull(err);
assert.equals(data, "file content");
// since yields is synchronous, anotherFake is not called yet
assert.isFalse(anotherFake.called);

sinon.restore();
});

anotherFake();
});
23 changes: 23 additions & 0 deletions docs/release-source/release/examples/fakes-08-yields-async.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;
const fs = require("fs");

it("should create a fake that 'yields asynchronously'", function () {
const fake = sinon.fake.yieldsAsync(null, "file content");
const anotherFake = sinon.fake();

sinon.replace(fs, "readFile", fake);
fs.readFile("somefile", (err, data) => {
// called with fake values given to yields as arguments
assert.isNull(err);
assert.equals(data, "file content");
// since yields is asynchronous, anotherFake is called first
assert.isTrue(anotherFake.called);

sinon.restore();
});

anotherFake();
});
18 changes: 18 additions & 0 deletions docs/release-source/release/examples/fakes-09-callback.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require("@fatso83/mini-mocha").install();
const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

it("should have working callback", function () {
const f = sinon.fake();
const cb1 = function () {};
const cb2 = function () {};

f(1, 2, 3, cb1);
f(1, 2, 3, cb2);

assert.isTrue(f.callback === cb2);
// spy call methods:
assert.isTrue(f.getCall(1).callback === cb2);
assert.isTrue(f.lastCall.callback === cb2);
});

This file was deleted.

16 changes: 7 additions & 9 deletions docs/release-source/release/examples/fakes-10-firstArg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

describe("FakeTest", function () {
it("should have working firstArg", function () {
const f = sinon.fake();
const date1 = new Date();
const date2 = new Date();
it("should have working firstArg", function () {
const f = sinon.fake();
const date1 = new Date();
const date2 = new Date();

f(date1, 1, 2);
f(date2, 1, 2);
f(date1, 1, 2);
f(date2, 1, 2);

assert.isTrue(f.firstArg === date2);
});
assert.isTrue(f.firstArg === date2);
});
24 changes: 11 additions & 13 deletions docs/release-source/release/examples/fakes-11-lastArg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

describe("FakeTest", function () {
it("should have working lastArg", function () {
const f = sinon.fake();
const date1 = new Date();
const date2 = new Date();
it("should have working lastArg", function () {
const f = sinon.fake();
const date1 = new Date();
const date2 = new Date();

f(1, 2, date1);
f(1, 2, date2);
f(1, 2, date1);
f(1, 2, date2);

assert.isTrue(f.lastArg === date2);
// spy call methods:
assert.isTrue(f.getCall(0).lastArg === date1);
assert.isTrue(f.getCall(1).lastArg === date2);
assert.isTrue(f.lastCall.lastArg === date2);
});
assert.isTrue(f.lastArg === date2);
// spy call methods:
assert.isTrue(f.getCall(0).lastArg === date1);
assert.isTrue(f.getCall(1).lastArg === date2);
assert.isTrue(f.lastCall.lastArg === date2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ const sinon = require("sinon");
const referee = require("@sinonjs/referee");
const assert = referee.assert;

describe("FakeTest", function () {
it("should be able to be added to the system under test", function () {
const fake = sinon.fake.returns("42");
it("should be able to be added to the system under test", function () {
const fake = sinon.fake.returns("42");

sinon.replace(console, "log", fake);
sinon.replace(console, "log", fake);

assert.equals(console.log("apple pie"), 42);
assert.equals(console.log("apple pie"), "42");

// restores all replaced properties set by sinon methods (replace, spy, stub)
sinon.restore();
// restores all replaced properties set by sinon methods (replace, spy, stub)
sinon.restore();

assert.equals(console.log("apple pie"), undefined);
assert.equals(fake.callCount, 1);
});
assert.isUndefined(console.log("apple pie"));
assert.equals(fake.callCount, 1);
});

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions docs/release-source/release/examples/fakes-5-returns.test.js

This file was deleted.

13 changes: 0 additions & 13 deletions docs/release-source/release/examples/fakes-6-throws.test.js

This file was deleted.

23 changes: 0 additions & 23 deletions docs/release-source/release/examples/fakes-7-yields.test.js

This file was deleted.

23 changes: 0 additions & 23 deletions docs/release-source/release/examples/fakes-8-yields-async.test.js

This file was deleted.

Loading

0 comments on commit 209f71f

Please sign in to comment.