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

fix(vitest-pool-workers): add node lib timers implementation #6444

Closed

Conversation

hansottowirtz
Copy link

@hansottowirtz hansottowirtz commented Aug 8, 2024

What this PR solves / how to test

Adds node lib timers implementation, so that the following code works:

require('node:timers').setTimeout

setImmediate, active, unroll, and promises are not implemented.

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because: they weren't there before, but I'm willing to add some
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required / Maybe required
    • Not necessary because: not relevant for this type of fix
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: wasn't there before

@hansottowirtz hansottowirtz requested a review from a team as a code owner August 8, 2024 08:28
Copy link

changeset-bot bot commented Aug 8, 2024

⚠️ No Changeset found

Latest commit: ef8c2f5

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

@petebacondarwin
Copy link
Contributor

If it is this simple, I wonder why @mrbbot didn't add this in the first place?

Copy link
Contributor

github-actions bot commented Aug 8, 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/10298648616/npm-package-wrangler-6444

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

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

Or you can use npx with this latest build directly:

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

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


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240725.0
workerd 1.20240806.0 1.20240806.0
workerd --version 1.20240806.0 2024-08-06

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

@mrbbot
Copy link
Contributor

mrbbot commented Aug 8, 2024

I wonder why @mrbbot didn't add this in the first place?

I wanted to minimise the additional Node polyfills to just those required by Vitest to reduce the likelihood of behaviour mismatches with regular wrangler dev/production. Ideally, these additional polyfills would only be importable by Vitest code. If Vitest now requires node:timers to be partially implemented, this PR seems reasonable. Otherwise, I'd try keep these additional polyfills as slim as possible.

@hansottowirtz
Copy link
Author

Thanks for your quick replies! We're trying to test our worker, and before deployment, we use esbuild to resolve node:timers, node:fs etc (using https://github.com/bubblydoo/cloudflare-workers-compat).
However, in tests, we preferably just import our worker directly from typescript without a building step (to have faster refreshes etc).
Unfortunately if we do:

defineWorkersConfig({
  ...,
  resolve: {
    alias: {
       timers: './aliases/timers.cjs',
       'node:timers': './aliases/timers.cjs',
    }
  }
})

Then, the worker/lib/node/timers.cjs file will still get priority instead of ./aliases/timers.cjs.

It would be great if the files in lib/node can only be imported by Vitest code. That would also fix #6405.

@hansottowirtz hansottowirtz marked this pull request as draft August 8, 2024 15:55
@petebacondarwin
Copy link
Contributor

So if we change the resolution to check the aliases before the built-in modules that would solve it for you?

@hansottowirtz
Copy link
Author

That would fix this specific issue, but not the others I've opened. Also as mrbbot mentioned the lib files being available in tests is quite a big difference between tests & production.

There are multiple things that can be improved in our situation:

  • configuring bundleDependencies
  • configuring esbuild settings
  • vitest-specific lib (e.g. fs, timers, debug)
  • bundling configuration that's shared between tests & wrangler deploy

I will look at the codebase and try to see what I can do, and then I will get back to this PR, maybe with a more holistic solution 🙂

@CarmenPopoviciu CarmenPopoviciu added the awaiting reporter response Needs clarification or followup from OP label Aug 16, 2024
@andyjessop
Copy link
Contributor

I'm closing this for now, as node:timers has just been implemented in workerd (https://github.com/cloudflare/workerd/releases/tag/v1.20250120.0)

@andyjessop andyjessop closed this Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reporter response Needs clarification or followup from OP
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants