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

Avoid JSON.stringify hack when testing responses of fetch-mock #588

Closed
5 tasks done
oscard0m opened this issue Jul 15, 2023 · 7 comments
Closed
5 tasks done

Avoid JSON.stringify hack when testing responses of fetch-mock #588

oscard0m opened this issue Jul 15, 2023 · 7 comments
Assignees
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone
Projects

Comments

@ghost ghost added this to Inbox in JS Jul 15, 2023
@oscard0m
Copy link
Member Author

oscard0m commented Jul 15, 2023

I took a look into it.

The toStrictEqual is doing some customTesters internally. The one giving problems is typeEquality. Concretely, this comparison: a.constructor === b.constructor.

When using fetch-mock, the accepted type for response parameter in getOnce(matcher, response) are:

type MockResponse = Response | Promise<Response>
                        | number | Promise<number>
                        | string | Promise<string>
                        | {} | Promise<{}>
                        | MockResponseObject | Promise<MockResponseObject>;

According to this code comment:

  • object: As long as the object is not a MockResponseObject it is
    * converted into a json string and returned as the body of a 200 response

This means the constructor equal comparison is lost at this point.12

Considering these are mocks controlled by ourselves, the alternatives I come up with are:
a) to use a jest.toEqual
b) to keep the hack of JSON.stringify
c) try if passing a MockResponseObject or Response gets the right constructor and toStrictEqual works.

What do you think @octokit/extensibility-sdks @octokit/js-community ?

Footnotes

  1. https://github.com/gr2m/fetch-mock/blob/1bdac54e1b482398cca0af9658a500de23e72367/src/lib/fetch-handler.js#L268-L278

  2. https://github.com/gr2m/fetch-mock/blob/1bdac54e1b482398cca0af9658a500de23e72367/src/lib/response-builder.js#L113

@oscard0m oscard0m self-assigned this Jul 15, 2023
@nickfloyd nickfloyd added the Status: Up for grabs Issues that are ready to be worked on by anyone label Jul 17, 2023
@kfcampbell
Copy link
Member

I think I have a weak preference in favor of jest.ToEqual. I had to remind myself of the differences when looking at .toStrictEqual docs:

  • keys with undefined properties are checked, e.g. {a: undefined, b: 2} will not equal {b: 2};
  • undefined items are taken into account, e.g. [2] will not equal [2, undefined];
  • array sparseness is checked, e.g. [, 1] will not equal [undefined, 1];
  • object types are checked, e.g. a class instance with fields a and b will not equal a literal object with fields a and b.

None of those seem deal-breaking to me, though I'd be interested to hear others' thoughts.

@oscard0m
Copy link
Member Author

oscard0m commented Jul 18, 2023

Thumbs up reaction (👍🏽) for jest.ToEqual, and I can apply the changes here:

@gr2m
Copy link
Contributor

gr2m commented Jul 19, 2023

Thumbs up reaction (👍🏽) for jest.ToEqual, and I can apply the changes here:

👍🏼 from me, thank you @oscard0m for taking on this and other chore tasks 💛

oscard0m added a commit that referenced this issue Jul 19, 2023
…ringify in Response assertions

More context in #588
oscard0m added a commit to octokit/rest.js that referenced this issue Jul 19, 2023
oscard0m added a commit to octokit/action.js that referenced this issue Jul 19, 2023
@wolfy1339 wolfy1339 changed the title Avoid JSON.stringify hack when testing responses of fetc-mock Avoid JSON.stringify hack when testing responses of fetch-mock Jul 19, 2023
@wolfy1339
Copy link
Member

There is at least one case where I switched to toMatchObject()

Those could probably be updated as well

@oscard0m
Copy link
Member Author

oscard0m commented Jul 19, 2023

There is at least one case where I switched to toMatchObject()

Those could probably be updated as well

In a quick search these are the ones I found:

The rest can be progressively updated. It's not a problem to have jest.toMatchObject().

oscard0m added a commit that referenced this issue Jul 19, 2023
…ringify in Response assertions (#589)

More context in #588
oscard0m added a commit to octokit/rest.js that referenced this issue Jul 19, 2023
#329)

<!-- Issues are required for both bug fixes and features. -->
Resolves #328

----

## Behavior

### Before the change?
<!-- Please describe the current behavior that you are modifying. -->
Test assertions of Responses from `fetch-mock` are done with
`jest.toStrictEqual` with `JSON.stringify`

### After the change?
<!-- Please describe the behavior or changes that are being added by
this PR. -->
Test assertions of Responses from `fetch-mock` are done with
`jest.toEqual`


### Other information
<!-- Any other information that is important to this PR  -->
More context in octokit/core.js#588

----

## Additional info

### Pull request checklist
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been reviewed and added / updated if needed (for bug
fixes / features)
- [ ] Added the appropriate label for the given change

### Does this introduce a breaking change?
<!-- If this introduces a breaking change make sure to note it here any
what the impact might be -->

Please see our docs on [breaking
changes](https://github.com/octokit/.github/blob/main/community/breaking_changes.md)
to help!

- [ ] Yes (Please add the `Type: Breaking change` label)
- [x] No

If `Yes`, what's the impact:

* N/A


### Pull request type
`Type: Maintenance`

----
oscard0m added a commit to octokit/action.js that referenced this issue Jul 19, 2023
oscard0m added a commit to octokit/oauth-app.js that referenced this issue Jul 19, 2023
oscard0m added a commit to octokit/oauth-app.js that referenced this issue Jul 19, 2023
test(refactor): use 'toEqual' instead of 'toMatchObje…

more context in octokit/core.js#588
@oscard0m
Copy link
Member Author

Thank you all for the quick reviews. We are done with this. 🥳 🎆 🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone
Projects
Archived in project
JS
  
Inbox
Development

No branches or pull requests

5 participants