wheresrhys/fetch-mock

Feature request: Implement `ok` `boolean` response field

Closed this issue · 7 comments

I'm not sure, but I can't find a way to produce ok boolean value in response object. https://developer.mozilla.org/en-US/docs/Web/API/Response/ok

ok is set automatically whenever you send a response with a status from 200-299:

The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.

I still do not get it, here are couple of examples with latest 12.0.2

  • response for status 200
{
      [Symbol(realm)]: { settingsObject: {} },
      [Symbol(state)]: {
        aborted: false,
        rangeRequested: false,
        timingAllowPassed: false,
        requestIncludesCredentials: false,
        type: 'default',
        status: 200,
        timingInfo: null,
        cacheState: '',
        statusText: 'OK',
        headersList: HeadersList {
          cookies: null,
          [Symbol(headers map)]: [Map],
          [Symbol(headers map sorted)]: null
        },
        urlList: [],
        body: {
          stream: undefined,
          source: '{"body":"Some invalid json","statusText":"DDDDD"}',
          length: 49
        }
      },
      [Symbol(headers)]: HeadersList {
        cookies: null,
        [Symbol(headers map)]: Map(2) { 'content-length' => [Object], 'content-type' => [Object] },
        [Symbol(headers map sorted)]: null
      }
    }
  • response for status 550
{
      [Symbol(realm)]: { settingsObject: {} },
      [Symbol(state)]: {
        aborted: false,
        rangeRequested: false,
        timingAllowPassed: false,
        requestIncludesCredentials: false,
        type: 'default',
        status: 550,
        timingInfo: null,
        cacheState: '',
        statusText: '',
        headersList: HeadersList {
          cookies: null,
          [Symbol(headers map)]: [Map],
          [Symbol(headers map sorted)]: null
        },
        urlList: []
      },
      [Symbol(headers)]: HeadersList {
        cookies: null,
        [Symbol(headers map)]: Map(1) { 'content-length' => [Object] },
        [Symbol(headers map sorted)]: null
      }
    }

I do not see any ok: true/false.
In fact I need to test code like this:

	const response: Response = await fetch(url, requestInit);
	if (!response.ok) {
		console.log(response.statusText);
	}

Can you tell me a bit more about your setup please, node version, or is this in a browser?, and any libraries you are using as part of your fetching. The sample you've logged out doesn't look like a native fetch Response, which is what fetch-mock creates.

I have node v20.11.0. I'm using fetch-mock inside jest tests with preset "ts-jest" in jest.config.ts and without specifying testEnvironment: "jsdom" which means node environment. Maybe thats the reason? But I thought fetch-mock must totally replace content returning by the native fetch no matter testing environment.

It does, but it uses the native fetch classes to do so, so you should receive a genuine Response, and the ok property should always exist on it.

I'm happy to investigate further, but I'll need you to create a repo with a reduced test case in it so I can reproduce locally

Thank you for clarification, and great project! I'll try to find out myself and will back here if find something interesting.

Closing due to inactivity. Happy to open and look into it if you can provide a reduced test case repo @Elmaranin