How to prevent server execution of Meteor methods in test scripts


I have some client test scripts that go beyond just a simple unit test (edging towards an integration test) where you can stub out meteor method calls. For example, I have routines that call Meteor methods from within. I need to take advantage of the client-side simulation of those methods but I do not want the method to make a call to the server.

Anyone know how I can stub that out? (it seems a simple requirement but I can’t for the life of me find a simple solution)


Are your methods manipulating Collections? If so one option you could consider is using hwillson:stub-collections to stub out your real Collections with in-memory local Collections. You’d still be making your Method’s server calls, but you wouldn’t be impacting your real Collections.


Yes, I use wilson stubs. And that works fine. It’s more of a performance/annoyance perspective to be honest.

My methods are secure and while I can get past the validation on the client they fail on the server and I get a ream of ‘unauthorised’ message logs back from the server as my tests progress. Just wanted to get rid of the noise and unnecessary activity.


Got it, makes sense. That’s one of the reason’s why I like using mdg:validated-method’s instead. It’s really easy to simulate calling a Method on behalf of a specific user from test code.


Yep, I use that too :smile:

Seems to me that it would be nice simply to turn off DDP for the duration of the tests - which you can do with Meteor.disconnect(). But sadly you have to turn it back on again eventually and … whoosh everything gets sent up.


I have an answer to this, by the way, should anyone be interested.

To recap, for client side tests there are many cases where I want to rely on/benefit from Meteor method simulation without having to stub those methods out. But a call to the server to execute the method ‘for real’ is not wanted or required.

My fix for this:

        beforeEach(function() {
            sinon.stub(DDP._CurrentInvocation,'get',() => {
                return { isSimulation: true};

        afterEach(function() {

It basically fools the DDP client into thinking the server call’s already been made!