Jsforce: Mocking/Stubbing

Created on 13 May 2014  路  4Comments  路  Source: jsforce/jsforce

Is there anyway to mock/stub a salesforce server so I can write tests that don't reach out to the network?

I'm currently using a stub API server via restify that simply returns stub responses, and pass that URL to the Connection constructor, (for stubbing my custom apexrest endpoints that live on salesforce servers) but I'm not quite clear what endpoints I need to mock in order to simulate the responses JSForce will receive when I execute SObject queries, for example.

enhancement

Most helpful comment

For anyone else hitting this page looking for something similar, I solved for this by stubbing out the individual accessor function after connection as follows:

sinon.stub(jsforce.Connection.prototype, 'create', function () {
  arguments[3](null, { success: true });
});

For context, I am testing a Node v6.2.0 API using Sails v1.0.0-19. Also, I am using ES6, but usage of arrow functions changes the meaning of arguments.

This gives fine grained control of the touch points that my code has with this library. In other words, I can easily introduce an error case with one parameter change to fully exercise every code path my controller expresses. Admittedly, I've not had to work with anything other than the 'tooling' functions, and this approach may not work outside those features.

All 4 comments

That is true for unit testing nature, but I'm feeling that creating mocks for all APIs and invocation patterns is a little impractical and increases the cost to maintain test cases.

Theoretically, you can use sinon.js or something to create a fake server and provide API mock response, but it might be much easier to signup new developer edition organization and point to the API connection to that org.

To set the connection to your own organization, put .env file in the project root, and write environment variables for the connection in it.

[email protected]
SF_PASSWORD=yourPassword
SF_OAUTH2_CLIENT_ID=ABCDEFG.......XYZ.0000
SF_OAUTH2_CLIENT_SECRET=999999999999999999
SF_AJAX_PROXY_URL=https://node-salesforce-proxy.herokuapp.com/proxy

For anyone else hitting this page looking for something similar, I solved for this by stubbing out the individual accessor function after connection as follows:

sinon.stub(jsforce.Connection.prototype, 'create', function () {
  arguments[3](null, { success: true });
});

For context, I am testing a Node v6.2.0 API using Sails v1.0.0-19. Also, I am using ES6, but usage of arrow functions changes the meaning of arguments.

This gives fine grained control of the touch points that my code has with this library. In other words, I can easily introduce an error case with one parameter change to fully exercise every code path my controller expresses. Admittedly, I've not had to work with anything other than the 'tooling' functions, and this approach may not work outside those features.

We did following and it seems to be working fine.

Stub the jsforce

sandbox.stub(jsforce)

Specify what happens when connection is created

jsforce.Connection.returns({
  query: (soql) => {
    const result = {
      records: [
        ... your records data
      ],
    };

    return Promise.resolve(result);
  },
});

Has anyone gotten Jest to work with jsforce? I don't think it can automock and I am having trouble trying to mock it manually.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sureshshamanth picture sureshshamanth  路  6Comments

JonDum picture JonDum  路  7Comments

rchaser53 picture rchaser53  路  6Comments

MGarfOppLoans picture MGarfOppLoans  路  7Comments

shomanishikawa picture shomanishikawa  路  7Comments