Skip to content

Commit 209f71f

Browse files
authored
Fix fake tests (#2439)
* Fix failing test * Flatten fake tests and restore sort order * cleanup * Update docs to reflect new structure * Fix formatting
1 parent 7f16cec commit 209f71f

24 files changed

+183
-213
lines changed

docs/CONTRIBUTING.md

-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ When you're contributing documentation changes for code in `master` branch, then
88

99
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.
1010

11-
### Example: documenting a fixed bug
12-
13-
Let us say that you are documenting a bug in `v1.17.1` that was fixed in `v1.17.4`.
14-
15-
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`.
16-
1711
## Running the documentation site locally
1812

1913
For casual improvements to the documentation, this shouldn't really be necessary, as all the content documents are plain markdown files.

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ published: false
66

77
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.
88

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

1111
## Documentation release process
1212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should be able to be used instead of spies", function () {
7+
const foo = {
8+
bar: () => "baz",
9+
};
10+
// wrap existing method without changing its behaviour
11+
const fake = sinon.replace(foo, "bar", sinon.fake(foo.bar));
12+
13+
assert.equals(fake(), "baz"); // behaviour is the same
14+
assert.equals(fake.callCount, 1); // calling information is saved
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should be able to be used instead of stubs", function () {
7+
const foo = {
8+
bar: () => "baz",
9+
};
10+
// replace method with a fake one
11+
const fake = sinon.replace(foo, "bar", sinon.fake.returns("fake value"));
12+
13+
assert.equals(fake(), "fake value"); // returns fake value
14+
assert.equals(fake.callCount, 1); // saves calling information
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should create fake without behaviour", function () {
7+
// create a basic fake, with no behavior
8+
const fake = sinon.fake();
9+
10+
assert.isUndefined(fake()); // by default returns undefined
11+
assert.equals(fake.callCount, 1); // saves call information
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should create fake with custom behaviour", function () {
7+
// create a fake that returns the text "foo"
8+
const fake = sinon.fake.returns("foo");
9+
10+
assert.equals(fake(), "foo");
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should create a fake that 'returns'", function () {
7+
const fake = sinon.fake.returns("apple pie");
8+
9+
assert.equals(fake(), "apple pie");
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should create a fake that 'throws'", function () {
7+
const fake = sinon.fake.throws(new Error("not apple pie"));
8+
9+
// Expected to throw an error with message 'not apple pie'
10+
assert.exception(fake, { name: "Error", message: "not apple pie" });
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
const fs = require("fs");
6+
7+
it("should create a fake that 'yields'", function () {
8+
const fake = sinon.fake.yields(null, "file content");
9+
const anotherFake = sinon.fake();
10+
11+
sinon.replace(fs, "readFile", fake);
12+
fs.readFile("somefile", (err, data) => {
13+
// called with fake values given to yields as arguments
14+
assert.isNull(err);
15+
assert.equals(data, "file content");
16+
// since yields is synchronous, anotherFake is not called yet
17+
assert.isFalse(anotherFake.called);
18+
19+
sinon.restore();
20+
});
21+
22+
anotherFake();
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
const fs = require("fs");
6+
7+
it("should create a fake that 'yields asynchronously'", function () {
8+
const fake = sinon.fake.yieldsAsync(null, "file content");
9+
const anotherFake = sinon.fake();
10+
11+
sinon.replace(fs, "readFile", fake);
12+
fs.readFile("somefile", (err, data) => {
13+
// called with fake values given to yields as arguments
14+
assert.isNull(err);
15+
assert.equals(data, "file content");
16+
// since yields is asynchronous, anotherFake is called first
17+
assert.isTrue(anotherFake.called);
18+
19+
sinon.restore();
20+
});
21+
22+
anotherFake();
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require("@fatso83/mini-mocha").install();
2+
const sinon = require("sinon");
3+
const referee = require("@sinonjs/referee");
4+
const assert = referee.assert;
5+
6+
it("should have working callback", function () {
7+
const f = sinon.fake();
8+
const cb1 = function () {};
9+
const cb2 = function () {};
10+
11+
f(1, 2, 3, cb1);
12+
f(1, 2, 3, cb2);
13+
14+
assert.isTrue(f.callback === cb2);
15+
// spy call methods:
16+
assert.isTrue(f.getCall(1).callback === cb2);
17+
assert.isTrue(f.lastCall.callback === cb2);
18+
});

docs/release-source/release/examples/fakes-1-using-fakes-instead-of-spies.test.js

-17
This file was deleted.

docs/release-source/release/examples/fakes-10-firstArg.test.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ const sinon = require("sinon");
33
const referee = require("@sinonjs/referee");
44
const assert = referee.assert;
55

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();
6+
it("should have working firstArg", function () {
7+
const f = sinon.fake();
8+
const date1 = new Date();
9+
const date2 = new Date();
1110

12-
f(date1, 1, 2);
13-
f(date2, 1, 2);
11+
f(date1, 1, 2);
12+
f(date2, 1, 2);
1413

15-
assert.isTrue(f.firstArg === date2);
16-
});
14+
assert.isTrue(f.firstArg === date2);
1715
});

docs/release-source/release/examples/fakes-11-lastArg.test.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ const sinon = require("sinon");
33
const referee = require("@sinonjs/referee");
44
const assert = referee.assert;
55

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();
6+
it("should have working lastArg", function () {
7+
const f = sinon.fake();
8+
const date1 = new Date();
9+
const date2 = new Date();
1110

12-
f(1, 2, date1);
13-
f(1, 2, date2);
11+
f(1, 2, date1);
12+
f(1, 2, date2);
1413

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-
});
14+
assert.isTrue(f.lastArg === date2);
15+
// spy call methods:
16+
assert.isTrue(f.getCall(0).lastArg === date1);
17+
assert.isTrue(f.getCall(1).lastArg === date2);
18+
assert.isTrue(f.lastCall.lastArg === date2);
2119
});

docs/release-source/release/examples/fakes-12-adding-fake-to-system-under-test.test.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ const sinon = require("sinon");
33
const referee = require("@sinonjs/referee");
44
const assert = referee.assert;
55

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");
6+
it("should be able to be added to the system under test", function () {
7+
const fake = sinon.fake.returns("42");
98

10-
sinon.replace(console, "log", fake);
9+
sinon.replace(console, "log", fake);
1110

12-
assert.equals(console.log("apple pie"), 42);
11+
assert.equals(console.log("apple pie"), "42");
1312

14-
// restores all replaced properties set by sinon methods (replace, spy, stub)
15-
sinon.restore();
13+
// restores all replaced properties set by sinon methods (replace, spy, stub)
14+
sinon.restore();
1615

17-
assert.equals(console.log("apple pie"), undefined);
18-
assert.equals(fake.callCount, 1);
19-
});
16+
assert.isUndefined(console.log("apple pie"));
17+
assert.equals(fake.callCount, 1);
2018
});

docs/release-source/release/examples/fakes-2-using-fakes-instead-of-stubs.test.js

-21
This file was deleted.

docs/release-source/release/examples/fakes-3-creating-without-behaviour.test.js

-14
This file was deleted.

docs/release-source/release/examples/fakes-4-creating-with-custom-behaviour.test.js

-13
This file was deleted.

docs/release-source/release/examples/fakes-5-returns.test.js

-12
This file was deleted.

docs/release-source/release/examples/fakes-6-throws.test.js

-13
This file was deleted.

docs/release-source/release/examples/fakes-7-yields.test.js

-23
This file was deleted.

docs/release-source/release/examples/fakes-8-yields-async.test.js

-23
This file was deleted.

0 commit comments

Comments
 (0)