Skip to content

vitest-pool-workers: Support AbortSignal in fetch-mock#7032

Merged
emily-shen merged 2 commits intocloudflare:mainfrom
Codex-:abortsignal_fetch_mock
Nov 27, 2024
Merged

vitest-pool-workers: Support AbortSignal in fetch-mock#7032
emily-shen merged 2 commits intocloudflare:mainfrom
Codex-:abortsignal_fetch_mock

Conversation

@Codex-
Copy link
Contributor

@Codex- Codex- commented Oct 21, 2024

What this PR solves / how to test

This PR introduces support for AbortSignal to fetch-mock, allowing signals to 'abort' requests during tests as they do at actual runtime. This makes it significantly easier to test this behaviour when writing tests that rely on signals and their behaviours.

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
      • Unit tests cover changes
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Changeset included
    • Changeset not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because:
      • This not being supported was a little cryptic as it was undocumented but suggested in the discord community to use AbortSignal when wanting to time out fetch calls. Supported AbortSignal aligns with what is expected of fetch in tests reflecting actual usage.

@Codex- Codex- requested a review from a team as a code owner October 21, 2024 04:42
@changeset-bot
Copy link

changeset-bot bot commented Oct 21, 2024

🦋 Changeset detected

Latest commit: 9794a39

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

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

/**
* Mutate an Error instance so it passes either of the checks in isAbortError
*/
export function castAsAbortError(err: Error): Error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lifted from: https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/utils/isAbortError.ts#L19-L20

Guessing this would be preferred over making additions to the wrangler exports

@Codex- Codex- force-pushed the abortsignal_fetch_mock branch from 87dcaec to 7eb5b47 Compare October 21, 2024 19:57
@lrapoport-cf lrapoport-cf added the caretaking Priority for caretaking label Oct 24, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-wrangler-7032

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7032/npm-package-wrangler-7032

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-wrangler-7032 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-create-cloudflare-7032 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-kv-asset-handler-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-miniflare-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-pages-shared-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-vitest-pool-workers-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-workers-editor-shared-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-workers-shared-7032
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11941549222/npm-package-cloudflare-workflows-shared-7032

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.88.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20241106.0
workerd 1.20241106.1 1.20241106.1
workerd --version 1.20241106.1 2024-11-06

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@Codex-
Copy link
Contributor Author

Codex- commented Oct 31, 2024

@emily-shen is there any chance I can get a re-review on this? :)

@emily-shen
Copy link
Contributor

Hey, really sorry for the delay! I had a look earlier and it looks all good to me, but I'm not actually that familiar with this part of the codebase and probably not the best person to review this, will forward this to the team again 😅

cc @penalosa (when you get back) and @edmundhung if you could help with this PR :)

@andyjessop
Copy link
Contributor

@Codex- Are we sure this is the correct behaviour in the runtime? This PR seems to suggest that the AbortController does not work as expected at the moment: #6467

@Codex- Codex- force-pushed the abortsignal_fetch_mock branch from d2bc6ac to acd1ed5 Compare November 12, 2024 20:29
@Codex-
Copy link
Contributor Author

Codex- commented Nov 12, 2024

@Codex- Are we sure this is the correct behaviour in the runtime? This PR seems to suggest that the AbortController does not work as expected at the moment: #6467

@andyjessop the linked appears to be in regards to a client aborting the request to the worker, but this is for a request the worker is making to another service via fetch which in my manual testing appears to work okay 🤔

@Codex-
Copy link
Contributor Author

Codex- commented Nov 18, 2024

 FAIL  e2e-tests/cli.test.ts > E2E: Basic C3 functionality  > Inferring the category, type and language if the type is `hello-world-python`
Error: Test timed out in 600000ms.

Appears unrelated to these changes

Validation errors in PR description:
-  Your PR must run E2E tests, or provide justification for why running them is not required
-  Your PR must include documentation (in the form of a link to a Cloudflare Docs issue or PR), or provide justification for why no documentation is required

e2e runs, and the documentation point is covered above

Is there anything else you need from me?

@Codex- Codex- force-pushed the abortsignal_fetch_mock branch from acd1ed5 to e805ea8 Compare November 20, 2024 10:43
@Codex- Codex- force-pushed the abortsignal_fetch_mock branch from e805ea8 to 9794a39 Compare November 20, 2024 20:52
Copy link
Contributor

@andyjessop andyjessop left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@andyjessop andyjessop added the e2e Run wrangler + vite-plugin e2e tests on a PR label Nov 27, 2024
@emily-shen emily-shen added the skip-pr-description-validation Skip validation of the required PR description format label Nov 27, 2024
@emily-shen emily-shen merged commit a18ed4e into cloudflare:main Nov 27, 2024
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes an open-source contribution, big or small label Nov 27, 2024
@holopin-bot
Copy link

holopin-bot bot commented Nov 27, 2024

Congratulations @Codex-, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cm3zxwf8718910cmnohdto81a

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

caretaking Priority for caretaking contribution [Holopin] Recognizes an open-source contribution, big or small e2e Run wrangler + vite-plugin e2e tests on a PR skip-pr-description-validation Skip validation of the required PR description format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants