BedrockStreaming/superagent-mock

Mocking an error and using retry()

techandsoftware opened this issue · 4 comments

I'm trying to do the following

  1. Get a fixture to return a server error (500)
  2. Use retry(3) on the superagent
  3. See that the fixture is called 3 times, and the get request ultimately fails.

I've written a small test script, from what it looks like the failure happens immediately after the exeption even though the retry callback is called 3 times afterwards.

Here's my script

var request = require('superagent');
var config = [{
  pattern: 'http://localhost/500/devices',
  fixtures: (match, params) => {
    let err = new Error(500);
    err.status = 500;
    throw err;
  },
  get: (match, data) => {
      return data;
  }
}];


var superagentMock = require('superagent-mock')(request, config);

request
  .get('http://localhost/500/devices')
  .retry(3, retryCallback)
  .then(res => {
    console.log('completed');
   	console.log(res);
  })
  .catch(err => {
    console.log('failed');
    console.log(err);
  });


function retryCallback(err, res) {
  console.debug('__retrying');
  return true;
}

Output is
failed
__retrying
__retrying
__retrying

Is there an error with how superagent-mock interacts with retry or am I approaching this the wrong way?

Hi, did you test to throw the error in the get method instead in the fixtures?

Yes, the result is the same.

I think the problem is with superagent-mock that don't handle superagent's retry method. We don't use the retry feature of superagent, but feel free to open a PR to fix this bug 😉

I'll try and work it out :)