1

I have mocked my API-Respones with fetchMock (v.5.13.1). I have been working with it for a quite long time now and I didn't see this kind of behaviour yet.

I mocked two GET responses that are very similar.

fetchMock.get('glob:*/shippings/',"results":[
{"id": "1234", "status": "RELEASED", "foo": "bar"},
{"id": "5678", "status": "CREATED", "foo": "bar"},)

fetchMock.get('glob:*/shipping/myId1234',
{"id": "1234", "status": "RELEASED", "foo": "bar"})

Now, the first one works properly, but the second get returns me this error message:

fetch-mock.js:187 Uncaught TypeError: Invalid status RELEASED passed on response object. To respond with a JSON object that has status as a property assign the object to body e.g. {"body": {"status: "registered"}}

I have an assumption, that I cant mock some response that contains a status, because thats in a way a reserved attribute for status codes, but I am not quite sure and I cant find any similar errors online.

Aleks
  • 101
  • 1
  • 12

1 Answers1

1

For the second request to fetchMock, it assumes status to be one of the standard codes supplied as an integer. According to the docs, the config supplied to fetchMock expects the following params

If an object only contains properties from among those listed below it is used to configure a Response to return

body: String | Object

Set the Response body. See the non-config Object section of the docs below for behaviour when passed an Object

Server responded ok { token: 'abcdef' }

status: Integer

Set the Response status

200, 404, 503

headers: Object

Set the Response headers

{'Content-Type': 'text/html'}

redirectUrl: String

The url from which the Response should claim to originate from (to imitate followed directs). Will also set redirected: true on the response

throws: Error

Force fetch to return a Promise rejected with the value of throws

new TypeError('Failed to fetch')

However for a custom status attribute you can respond with a body

fetchMock.get('glob:*/shipping/myId1234', {
    body: {"id": "1234", "status": "RELEASED", "foo": "bar"}
})
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400