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

test: setup workerd isolate tests #311

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

test: setup workerd isolate tests #311

wants to merge 6 commits into from

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Sep 19, 2024

This PR adds isolated tests via workerd.

This setup uses a small Node.js wrapper that starts a module fallback server (based on cloudflare/workerd#1423) and then spawns workerd test to run tests.

The fallback server, maps unenv/runtime/* requests to source code (transpiled with esbuild to allow HMR). esbuild config uses env(nodeless, cloudflare) aliases to match the expected bundler behavior when resolving internal node: aliases.

As sample, two tests for crypto.getRandomValues and url.parse (fail behavior) are added. We shall iterate and add more coverage for runtime safety as we introduce new features.

Usage:

  • pnpm test:workerd
  • pnpm test:workerd --watch (watch mode)
  • DUMP_MODULES=1 pnpm test:workerd (dump server modules to test/workerd/.tmp for inspection)

@pi0 pi0 self-assigned this Sep 19, 2024
test/workerd/_server.mjs Outdated Show resolved Hide resolved
@pi0 pi0 requested review from jasnell, anonrig and a team September 19, 2024 17:39
import assert from "node:assert";
import process from "node:process";

globalThis.process = process;
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this, workerd fail:

workerd/io/worker.c++:1950: info: uncaught exception; source = Uncaught (in promise); stack = ReferenceError: process is not defined
    at unenv/runtime/node/crypto:387:21
workerd/io/io-context.c++:348: info: uncaught exception; exception = workerd/jsg/_virtual_includes/jsg/workerd/jsg/value.h:1367: failed: jsg.ReferenceError: process is not defined

(process is used for process.getBuiltinModule("node:crypto")...)

@jasnell @anonrig is this expected?

@pi0 pi0 marked this pull request as ready for review September 19, 2024 17:42
@pi0 pi0 requested a review from IgorMinar September 19, 2024 17:42
format: "esm",
target: "esnext",
platform: "node",
sourcemap: "inline",
Copy link
Member Author

Choose a reason for hiding this comment

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

It seems not being picked by workerd.

},
};

// ---- node:url ----
Copy link
Member Author

Choose a reason for hiding this comment

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

We have all Node.js upstream tests for url, in test/node/* now. But since this entry is inlined in capnp config, I'm not sure how to dynamically bring them. If we can, we can reuse same impl.

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.

1 participant