Skip to content

Return Array from .reply(200, 'return array instead of object or string'). #1208

@abhishekk-optimus

Description

@abhishekk-optimus

Hello,
i am working with mocha testing, and using nock to mock a get request which returns data in form of an
array.

3rd Part Api returns data (in form of array):

 [
  {
    "name": "Stamps.com",
    "code": "stamps_com",
    "accountNumber": "SS123",
    "requiresFundedAccount": true,
    "balance": 24.27,
    "nickname": null,
    "shippingProviderId": 12345,
    "primary": true
  }
 ]

So, to mock above data,
i am trying to do

let mockCarriers = [
  {
    "name": "Stamps.com",
    "code": "stamps_com",
    "accountNumber": "SS123",
    "requiresFundedAccount": true,
    "balance": 24.27,
    "nickname": null,
    "shippingProviderId": 12345,
    "primary": true
  },
 ]

nock(apiUrl).get('/carriers').reply(200, mockCarriers).persist();

But, instead of taking it as an array, it takes it as a string.
After researching, i found this : https://github.com/nock/nock#specifying-replies
that states the replies that can be set over reply method.

So, i am wondering, if this is possible to return array instead of object from the reply method ?

Open to suggestions.
Please suggest.
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportGeneral questions or support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions