Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Implement ok boolean response field #858

Open
Elmaranin opened this issue Oct 29, 2024 · 6 comments
Open

Feature request: Implement ok boolean response field #858

Elmaranin opened this issue Oct 29, 2024 · 6 comments

Comments

@Elmaranin
Copy link

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

@wheresrhys
Copy link
Owner

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.

@Elmaranin
Copy link
Author

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);
	}

@wheresrhys
Copy link
Owner

wheresrhys commented Oct 29, 2024

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.

@wheresrhys wheresrhys reopened this Oct 29, 2024
@Elmaranin
Copy link
Author

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.

@wheresrhys
Copy link
Owner

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

@Elmaranin
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants