Skip to content

chore(openapi-fetch): Split apart tests #1895

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

Merged
merged 2 commits into from
Sep 3, 2024
Merged

chore(openapi-fetch): Split apart tests #1895

merged 2 commits into from
Sep 3, 2024

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Sep 3, 2024

Changes

Fixes #1852. Splits apart tests from one mega file into easier-to-manage smaller files.

There are also a few other changes which don’t affect runtime at all, and should just ensure better test accuracy.

Updates TS to 5.5.4

There are some type errors that may take more work to catch, but this at least updates the test suite to the latest version.

Changes moduleResolution: "NodeNext"

This changes to NodeNext based on some feedback about using this in Node.js. Even though this utility is universal, and works in-browser and Node, enabling moduleResolution: "NodeNext" should just guarantee Node.js compatibility without losing browser compat.

Replacing MSW with custom fetch

This PR also reduces a lot of boilerplate and footguns by swapping MSW with custom fetch:

const client = createObservedClient(options, async (req) => {
  console.log(req);
  return Response.json({ success: true });
});

Most API integraiton tests consist of the following steps:

  1. Forming a request
  2. Request reaches the (mock) server
  3. (Mock) server returns response
  4. Response fires off side effects in the application

Most tests involving mock servers are more focused on step 4 (and beyond), so this is where MSW (and other fetch mock libraries) shine. In fact, one of the largest reasons for mocking libraries existing is to specifically remove the boilerplate of steps 1–3 (example: MSW). But our tests focus on steps 1 and 2 (rarely step 3 when testing middleware). This makes sense because most test needs are for applications, not fetch libraries! But the unintended side-effect of trying to get back the boilerplate that these libraries try to save you from is (you guessed it) more boilerplate.

Simply replacing fetch using the custom fetch API is the ideal solution. It not only lets us not worry about a centrally-mocked server reaching across all tests; it also gives us the thing we want which is the “boilerplate” of direct introspection—the thing that mocking libraries intentionally try and save you from. The end result is tests that are easier to write, easier to predict, and completely remove shared state and setup. And run faster and with less overhead.

There’s not really a danger we’re reducing coverage by not using globalThis.fetch; we’re just moving indirection. But even if there was some regression caused by this, we still have E2E tests in a headless browser that would catch it.

How to Review

  • Tests weren’t rewritten; only broken apart. In breaking apart tests, many duplicate tests were found that were removed.
  • Tests should pass
  • To minimize disruption (the longer this sits, the more PRs that may experience hard-to-resolve conflicts), I’m going to merge this. Happy to address any/all feedback post-merge!

Checklist

  • Tests pass
  • Documentation updated

Copy link

changeset-bot bot commented Sep 3, 2024

⚠️ No Changeset found

Latest commit: 5a633fb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@drwpow drwpow force-pushed the openapi-fetch-tests branch from b9e6a59 to f192035 Compare September 3, 2024 05:17
Copy link

cloudflare-workers-and-pages bot commented Sep 3, 2024

Deploying openapi-ts with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5a633fb
Status: ✅  Deploy successful!
Preview URL: https://72e6ba3f.openapi-ts.pages.dev
Branch Preview URL: https://openapi-fetch-tests.openapi-ts.pages.dev

View logs

Copy link
Contributor

github-actions bot commented Sep 3, 2024

size-limit report 📦

Path Size
packages/openapi-fetch/dist/index.min.js 5.85 KB (0%)

@drwpow drwpow force-pushed the openapi-fetch-tests branch 6 times, most recently from c778254 to 21a7fbf Compare September 3, 2024 16:14
@drwpow drwpow force-pushed the openapi-fetch-tests branch from 21a7fbf to fac5b79 Compare September 3, 2024 16:16
@drwpow drwpow changed the title [WIP] Split apart tests chore(openapi-fetch): Split apart tests Sep 3, 2024
@drwpow drwpow marked this pull request as ready for review September 3, 2024 16:23
@drwpow drwpow requested a review from a team as a code owner September 3, 2024 16:23
@drwpow drwpow merged commit 08e507d into main Sep 3, 2024
10 checks passed
@drwpow drwpow deleted the openapi-fetch-tests branch September 3, 2024 16:23
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.

Help wanted: split type & runtime tests for openapi-fetch
1 participant