1

I was using nock 9.1.6 to mock nylas API with chai 3.5.0. It was working perfectly with the code:

it('fails when no state is given and provider fails', () => {
  const user = fixture.elements.user1;
  const connector = new NylasConnector('nylas', 'connect', user);

  nock(NYLAS_API)
        .post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
        .reply(500, (uri, body) => expect(body).to.eql(''));

  sinon.stub(Nylas, 'urlForAuthentication').callsFake(params => {
    expect(params).to.eql({
      redirectURI: `${DN_AUTH_SERVICE_URL}/oauth2/nylas/callback`,
      trial: false,
      state: JSON.stringify({})
    });
    return chance.url() + '?' + objectToQuery(params);
  });

  return expect(connector.getAuthenticationUrl()).to.be.rejectedWith(ProviderError)
    .then(() => User.findById(user._id))
    .then(user => {
       expect(user.nylas).not.null;
       expect(user.nylasToken).not.null;
       expect(user.nylasCursor).not.null;
     });
  })

I tried to upgrade nock to 10.0.6 and chai to 4.2.0 and now the following error appears:

"Error: Invalid Chai property: setEncoding",
    "    at Object.proxyGetter [as get] (/home/jeremy/datananas/datananas/packages/api/node_modules/chai/lib/chai/utils/proxify.js:78:17)",
    "    at Object.isStream (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/common.js:382:24)",
    "    at continueWithResponseBody (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:435:20)",
    "    at end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:385:12)",
    "    at /home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:160:9",
    "    at OverriddenClientRequest.RequestOverrider.req.write (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:139:9)",
    "    at OverriddenClientRequest.RequestOverrider.req.end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:156:11)",
    "    at Request.end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:1514:14)",
    "    at end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:564:14)",
    "    at Immediate.<anonymous> (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:578:7)",
    "    at runCallback (timers.js:705:18)",
    "    at tryOnImmediate (timers.js:676:5)",
    "    at processImmediate (timers.js:658:5)"

The error appears on connector.getAuthenticationUrl() as it's the function who called the mocked function. In this function, their is a call to nylasRequest and the error appears when the line with the new Promise() is reached:

function nylasRequest(uri, token, body) {
  const params = {
    uri: `https://api.nylas.com${uri}`,
    method: 'POST',
    json: body
  };

  if (token)
    params.auth = { user: token };

  console.log('This line appears. All params are valid.')
  return new Promise((resolve, reject) => request(params, (error, res, body) =>  {
    console.log('This line never shows up');
    // some code
  }));
}

I use request 2.88.0 and I did not change the version I use.

Jérémy Pouyet
  • 1,989
  • 6
  • 28
  • 55

1 Answers1

0

I'm assuming your issue is with this line:

.reply(500, (uri, body) => expect(body).to.eql(''))

Nock is going to use the returned value from that function as the body of the fake response. Returning a chai expectation from the function seems to confuse nock when it tries to figure out if the response body is streamable.

I would change it to something like this:

nock(NYLAS_API)
  .post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
  .reply(500, (uri, body) => {
    expect(body).to.eql('')
    return ''
  });
Matt R. Wilson
  • 7,268
  • 5
  • 32
  • 48