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

Lines changed: 183 additions & 213 deletions

docs/CONTRIBUTING.md

Lines changed: 0 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 0 deletions
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+
});
Lines changed: 15 additions & 0 deletions
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+
});
Lines changed: 12 additions & 0 deletions
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+
});
Lines changed: 11 additions & 0 deletions
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+
});
Lines changed: 10 additions & 0 deletions
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+
});
Lines changed: 11 additions & 0 deletions
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+
});
Lines changed: 23 additions & 0 deletions
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+
});
Lines changed: 23 additions & 0 deletions
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+
});

0 commit comments

Comments
 (0)