Skip to content

Initialize JavaScript tests with polyfilled fetch#7518

Merged
aduth merged 1 commit intomainfrom
aduth-real-fetch
Dec 20, 2022
Merged

Initialize JavaScript tests with polyfilled fetch#7518
aduth merged 1 commit intomainfrom
aduth-real-fetch

Conversation

@aduth
Copy link
Contributor

@aduth aduth commented Dec 20, 2022

🛠 Summary of changes

Replaces default spec behavior of throwing on attempted fetch request with working polyfill.

This was originally planned for #7502, but blocked by failures in form-steps-wait-spec.tsx, addressed here.

The issue with the specs is that form-steps-wait.tsx has special handling for polyfill'd fetch, due to circumstances of real browsers handling of XMLHttpRequest. Since we are expecting to fully simulate the network request, we should bypass this workaround handling. (Aside: The workaround will be removed anyways as part of upcoming sunsetting of Internet Explorer support)

📜 Testing Plan

yarn test

changelog: Internal, Automated Tests, Improve JavaScript tests simulating network requests
@aduth aduth requested a review from allthesignals December 20, 2022 18:39
});

if ('polyfill' in window.fetch) {
if ((window.fetch as FetchOrFetchPolyfill).polyfill) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change from in to truthy check, since in will pass for the stubbed value of undefined.

The polyfill sets it to true (source).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh interesting. so if someone had polyfill: false it'd still pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the only way it could work previously is if we would delete the property altogether (delete fetch.polyfill), but there's not a clean with to stub that.

Copy link
Contributor

@allthesignals allthesignals left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome lgtm!

Change defaults test suite to native fetch (rather than defaulting to requiring a polyfill), making things future-friendly.

form: HTMLFormElement;
}

type FetchOrFetchPolyfill = typeof window.fetch & { polyfill?: boolean };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jw: what is this syntax? merge of the surface of window.fetch with an additional flag?

Copy link
Contributor Author

@aduth aduth Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, without this, TypeScript will become upset about trying to reference fetch.polyfill, since polyfill is not a property on the default fetch implementation, only in the polyfill.

So we typecast fetch to a type which effectively extends the default fetch type with the additional polyfill property.

Specifically, the syntax is an intersection type: https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types

});

if ('polyfill' in window.fetch) {
if ((window.fetch as FetchOrFetchPolyfill).polyfill) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh interesting. so if someone had polyfill: false it'd still pass.

@aduth aduth merged commit de3d7d5 into main Dec 20, 2022
@aduth aduth deleted the aduth-real-fetch branch December 20, 2022 19:32
@aduth aduth mentioned this pull request Dec 22, 2022
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

Successfully merging this pull request may close these issues.

2 participants