-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
RFC: Stubs & Spies #1825
Comments
I've published a library that implements (most of) this API here: https://github.com/jamiebuilds/ninos |
Hey this looks nice!
How do you see cleanup working? In my experience the spy / stub setup is impacted by test concurrency, and therefore so is cleanup. Could you elaborate? Would we be able to return better error messages without adding assertions? Note that we're looking at adding |
Just my 2 cents - my only issue with AVA+Sinon has been difficulty with concurrency. I agree that Sinon's API surface is pretty big, but their docs are sufficient (IMHO), and most folks have used it before. I find that I either have to run my tests in serial (which isn't great), or I have to use some form of dependency injection. I (almost) always go with the injection method, but in my experience, this often requires a pretty heavy refactoring of the app/service/thing I'm testing. If the outcome of this thread resolves that issue, it'd make AVA much easier to write tests using mocks. |
My library doesn't handle this well enough, but because This would also allow you to detect a few things:
I think we'd have to play around a bit with what warnings/errors are most useful and when, but we can do a lot more by hooking into internals. People will still likely have some amount of trouble with singletons, but I'm not sure that's ever going to be completely solved in JavaScript unless tests were completely isolated. |
I have to think about that more. I wasn't aware you were thinking about removing power assert from others. Maybe you could replace it with something lighter weight in other scenarios? Either way I think the experience is still pretty good without it being specialized (based on the errors I was getting with Niños). |
I'd like to explore what we'd need to support integration of libraries like Niños with AVA. We have an open issue to allow customizable assertions (#1094), which could be used to add |
All it would take to make this work as a separate library is to have a function ninos(test) {
test.beforeEach(t => {
t.context.spies = [];
t.context.spy = () => {
// ...
t.context.spies.push({ object, method, original });
};
});
test.afterEach(t => {
t.context.spies.forEach(({ object, method, original }) => {
object[method] = original;
});
});
} const test = require('ava');
require('ninos')(test);
test('spy', t => {
t.context.spy(object, 'method'); // reset automatically
}); This can be improved a lot with some sort of |
Separately, I would argue that this sort of thing should be built into the test framework itself for a nicer out of the box experience. I think that AVA should definitely have a I'm going to propose an API for extending the test execution context which can handle test concurrency. But I suggest we continue exploring adding stubs/spies to AVA. |
If I'm reading this correctly this could be handled by my
Currently I'd rather not add such features. We have plenty other challenges to tackle. |
Is the reason just that you're too busy? I would be happy to implement this |
I don't think AVA's core should say how to do stubs & mocks. I'd rather support bindings for the various libraries out there. Happy to host them in the GitHub org and publish under our npm scope. We do have to make that setup as friction free as possible. |
I for one would love to have a simple stubbing solution built-in. I find libraries like Sinon, while great, a bit too much and hard to use. The downside is that it's difficult to maintain a good stubbing library. There are lots of edge-cases to handle and I don't think we have the resources to handle that. We definitely should improve the extensibility of AVA though, making it super easy to integrate an external stubbing library. |
since I loved ninos api and this went nowhere, I created dummee, which is basically |
I wanted to quickly write this up because I think that AVA could provide a superior/simpler developer experience for stubbing within tests than tools like Sinon.
Instead of writing a detailed proposal, I'm going to give some API examples and we can refine them from there. I will also create a library that implements as much of this as possible.
Why add stubs/spies
Why not a separate AVA-specific library
Real World Examples
Detailed API Examples
API type signature
The text was updated successfully, but these errors were encountered: