mapbox/mapbox-sdk-js

mocking (directions) calls using nock

boredland opened this issue · 1 comments

hi there! I currently struggle to mock the directions API using nock, perhaps someone here has an idea how to achieve this?

you can generate a mock using nock quite easily by just inserting nock.recorder.rec(); in a jest test.

the generated code looks like this:

nock('https://api.mapbox.com:443', { encodedQueryParams: true })
  .get(
    '/directions/v5/mapbox/driving/8.767828%2C50.15727%3B8.761143%2C50.15661%3B8.759261..',
  )
  .query({
    alternatives: 'false',
    continue_straight: 'true',
    geometries: 'geojson',
    overview: 'full',
    steps: 'false',
    access_token:
      'pk.xxx.xxx',
  })
  .reply(
    200,
    [
      '1f8b0800000000000003cd5aed6e5bc7117d15.....gzipped response payload',
    ],
    [
      'Content-Type',
      'application/json; charset=utf-8',
      'Transfer-Encoding',
      'chunked',
      'Connection',
      'close',
      'Date',
      'Wed, 29 Jun 2022 17:34:29 GMT',
      'Access-Control-Allow-Origin',
      '*',
      'x-mapbox-request-id',
      'xxx==',
      'Cache-Control',
      'max-age=3600',
      'x-mapbox-routing-engine',
      'v2.0',
      'Content-Encoding',
      'gzip',
      'Vary',
      'Accept-Encoding',
      'X-Cache',
      'Hit from cloudfront',
      'Via',
      '1.1 xxx.cloudfront.net (CloudFront)',
      'X-Amz-Cf-Pop',
      'FRA50-C1',
      'X-Amz-Cf-Id',
      'xxx',
      'Age',
      '1657',
    ],
  );

while this mock catches the request as it should, the sdk emits just "Request aborted". So something is not like it expects it to be in the response. Any ideas?

my current assumption is, that this is mainly an issue with nock.