Skip to content
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

sinon.sandbox analog? #188

Closed
vovacodes opened this issue Feb 20, 2017 · 2 comments
Closed

sinon.sandbox analog? #188

vovacodes opened this issue Feb 20, 2017 · 2 comments

Comments

@vovacodes
Copy link

vovacodes commented Feb 20, 2017

Some test runners, e.g. ava, can execute your tests in parallel. Having a single "mocking" context might lead to race conditions when doing td.reset(). Is there a way of creating an isolated mocking context that could be restored independently of the other tests, like sinon.sandbox provides?

@searls
Copy link
Member

searls commented Feb 20, 2017

There is not, but if you consider what sinon.sandbox does and what td.reset applies to, I'm not sure there's much daylight for an additional feature to exist.

Sandbox is a really odd feature, that I think is more intended to (1) scope global sinon configuration down, and (2) give some way of resetting more than one double at a time (I don't believe there is a global sinon.reset, which was the major reason I'd see anyone using the sandbox feature at all.

In fact, take a look here for why sandbox doesn't actually solve the problem you suggest it would: avajs/ava#1066 -- as long as you're globally replacing something, you can't run tests concurrently safely, full stop.


So instead what I'd suggest is to either (1) mark your unit tests that use td.js to replace production-side code as .serial, or (2) avoid using td.replace at all for async tests and instead use traditional/manual dependency injection to get your fakes to your subject.

Fears of running serial as undercutting the value of ava are, I believe, unfounded if you're using td.js to write isolated unit tests. Since that means they're not doing any I/O, running them concurrently intra-process doesn't buy you much. Each file is still tested concurrently by ava in a separate process, but intra-file specs would be run serially for your isolated unit tests. And that advice applies to both Sinon & td.js

@searls searls closed this as completed Feb 20, 2017
@searls
Copy link
Member

searls commented Feb 20, 2017

I wrote more about this last year here: #131 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants