-
-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New API: default sandbox and fake #1586
Conversation
lib/sinon.js
Outdated
create: deprecated.wrap( | ||
createSandbox, | ||
// eslint-disable-next-line max-len | ||
"`sandbox.create()` is deprecated. Use default sandbox at `sinon.sandbox` or create new sandboxes with `sinon.createSandbox()` or `new sinon.Sandbox()`" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create new sandboxes with
sinon.createSandbox()
or `new sinon.Sandbox()
Why have two ways of creating sandboxes? We just made a new factory function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the second way and tidy up the documentation. Thanks
In the current version of the branch (5883852), that property is not exposed. > var sinon = require('./lib/sinon');
undefined
> typeof sinon.Sandbox
'undefined' |
7b2953a
to
dc8e063
Compare
695b75f
to
25c97fb
Compare
Ping @sinonjs/sinon-core This is getting close to being reviewable, however two of the tests are failing in Chromium. Any ideas why that might be? |
@mroderick Nice work! I had a look at the failing tests. It's |
docs/release-source/release/stubs.md
Outdated
@@ -178,6 +176,10 @@ This is equivalent to calling both `stub.resetBehavior()` and `stub.resetHistory | |||
|
|||
*Updated in `sinon@2.0.0`* | |||
|
|||
*Since `sinon@5.0.0`* | |||
|
|||
You can reset all stubs using `sinon.reset()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify what this resets. I assume it's behavior and history here, right?
docs/release-source/release/stubs.md
Outdated
|
||
*Since `sinon@5.0.0`* | ||
|
||
You can reset behaviour of all stubs using `sinon.resetHistory()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: resetHistory
doesn't reset behavior.
Also do we consistently use the Old English (British?) spelling of behavior in the docs? The actual method name uses the American English spelling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also do we consistently use the Old English (British?) spelling of behavior in the docs? The actual method name uses the American English spelling.
My natural language uses (mostly) British English spelling. Sometimes when writing code and documentation, that spelling slips through.
(I wonder if there's a spelling linter where you can set the language preference)
Tests are passing now (Travis was having a bit of a day). If you don't have any further comments for this PR, then I'll rewrite the history to read better. Do we need more documentation of |
test/fake-test.js
Outdated
|
||
function noop() {} | ||
|
||
var hasFunctionNameSupport = typeof noop.name !== "undefined"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noop.name === 'noop'
is a bit more straight forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This idea is great!
This solves all the problems we've had with inconsistent APIs and regressions and adds a better and simpler API at the same time.
I can't wait to get this on the codebase!
Awesome job.
I've left a few comments regarding the docs, but I still gotta review the code itself.
I was really busy in the last couple months because I was moving to London, so now that I'm all set up I will be able to come back and work with you some more 😄
(I missed y'all btw)
|
||
### Fakes with behaviour | ||
|
||
Fakes can be created with behaviour, which cannot be changed once the fake has been created. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it would be good to point out that it is possible to do a restore
as mentioned here and that would be enough for the user to get the original property back into the object.
As a user I think that would be something I'd like to know how to do as soon as I read that:
[a fake] cannot be changed once the fake has been created
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace
and restore
belong together. Fakes are created separately from replacing properties.
Fakes are just one possible argument to replace
, you could use something else if you wanted to (like mock
, spy
or stub
... or even Date
).
I'd like to keep the documentation focused, so not mention topics that are not necessary for understanding the current topic. With that in mind, I think that mentioning replace
in the documentation about creating fakes with behaviour, would reduce the focus and make the text a bit confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally understand, but then maybe we could rephrase this to make it more specific.
The first time I read this I had the strong impression that you would not be able to restore any property replaced with a fake.
The wording is a bit difficult though, what do y'all think? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucasfcosta I think the documentation already points out that replace
is a sandbox method that may use a fake as an argument. The standbox can then be restored. There is nothing to restore in the fake itself. I agree with @mroderick here that talking about restore
here would make the documentation more confusing.
docs/release-source/release/fakes.md
Outdated
// 42 | ||
``` | ||
|
||
When you want to restore the replaced properties, `sinon.restore` method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though this is really simple it might be worth having an example added.
Also, I'd add another header for this to make it as visible as possible given the importance of this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I've pushed new commits
|
||
Creates a sandbox object with spies, stubs, and mocks. | ||
Since `sinon@5.0.0`, the `sinon` object is a default sandbox. Unless you have a very advanced setup or need a special configuration, you probably want to just use that one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd also add a line here explaining why this change happened and how this makes testing easier, especially mentioning how useful it is to clear/restore all methods at once without having to create lots of sandboxes on each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I agreed with you, and wrote some documentation.
But then I thought about it some more. The motivation is only really interesting when you're transitioning from earlier versions to this MAJOR version. Once you're over that hump, stating that in the documentation just adds noise.
Instead, I've put details into the migration guide for v5, and have pushed a new commit with that.
|
||
`replacement` can be any value, including `spies`, `stubs` and `fakes`. | ||
|
||
This method only works on non-accessor properties, for replacing accessors, use `sandbox.replaceGetter()` and `sandbox.replaceSetter()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great idea btw.
Since we've been dealing with getters/setters
we've had a lot of trouble making the APIs generic enough to deal with them, but I guess that encapsulating that logic into separate methods is a lot better and allows us to avoid having lots of special conditions and a complex control-flow.
e9a978b
to
ed3c9dc
Compare
I've tried using this branch with an internal project with 1793 tests, most of them using Sinon. I am happy to report, that there were no problems. It would be great if someone else could take it for a spin, before we publish it as |
docs/guides/migrating-to-5.0.md
Outdated
|
||
describe('myFunction', function() { | ||
afterEach(function () { | ||
this.sandbox.restore(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You saw nothing! Nothing!
f3bc96e
to
5650f00
Compare
I also
All green 😄 Word of warning: I write really simple tests and I don't have access to projects from my previous employers that had much more elaborate setups. But at least initial impressions are good! And the fact that internal sinon tests pass is very promising. |
Thanks @fatso83. I'll try to find some time in near future to put a |
This is now on npm as |
b2353c5
to
9b10f93
Compare
b4e2234
to
a9a7cc0
Compare
IE doesn't have support for name property on Function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
Explain how to update to use the default sandbox
Inspired by https://remysharp.com/2018/01/10/open-source-with-a-cap-in-hand Implementation shamelessly copied from https://github.com/remy/nodemon/blob/9d498523d750496aba1fb0f44c2befe42cb9d9f7/package.json#L42
a9a7cc0
to
5767a33
Compare
This has been replaced by #1700 |
* Update tests to include bucket:create permission * Use sinon.createSandbox instead of sinon.sandbox.create This seems to be the first of many consequences from sinonjs/sinon#1586 and sinonjs/sinon#1700. I'm not sure what the final result of all of this will be -- we might not even need this sandbox any more, because a default one is created for us -- but at some point in the future, we'll probably have to rewrite everything that uses a stub(), and I prefer to wait until then to make any drastic changes. This just silences the warnings until then. * Try to make tests work on both kinto versions
This PR is a solution for the proposal in #1559
This PR should not be merged into
master
until we have given it a good trial in real world projects.Contributions (code, fixes, tests, documentation) are very welcome. Please make pull requests into this branch.
I'll keep the branch up to date by pulling from
master
at intervals. Once we're happy with the branch, then I'll rebase against master and get rid of the merge commits.Tasks
sinon
object a defaultsandbox
sandbox.replace
methodsandbox.replace
sandbox.replaceGetter
methodsandbox.replaceGetter
sandbox.replaceSetter
methodsandbox.replaceSetter
sinon.fake
fake
[.withArguments
, etc]sinon.fake
sinon@next
to get feedbackTo try it out in your project directly from source
git checkout default-sandbox-and-fake
npm link
npm link sinon
Once we start publishing release candidates on npm, then we'll add instructions for testing in your projects here.