import drain from 'it-drain';
export default function testTimeout(fn) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const start = Date.now();
      let res = fn();
      if (res[Symbol.asyncIterator]) {
        res = drain(res);
      }
      res.then(result => {
        const timeTaken = Date.now() - start;
        if (timeTaken < 100) {
          return resolve();
        }
        reject(new Error(`API call did not time out after ${ timeTaken }ms, got ${ JSON.stringify(result, null, 2) }`));
      }, err => {
        if (err.name === 'TimeoutError') {
          return resolve();
        }
        const timeTaken = Date.now() - start;
        reject(new Error(`Expected TimeoutError after ${ timeTaken }ms, got ${ err.stack }`));
      });
    }, 10);
  });
}