Skip to content

test: setup workerd isolate tests - #311

Merged
pi0 merged 8 commits into
mainfrom
test/workerd
Oct 7, 2024
Merged

test: setup workerd isolate tests#311
pi0 merged 8 commits into
mainfrom
test/workerd

Conversation

@pi0

@pi0 pi0 commented Sep 19, 2024

Copy link
Copy Markdown
Member

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
Comment thread test/workerd/_server.mjs Outdated
@pi0
pi0 requested review from a team, anonrig and jasnell September 19, 2024 17:39
Comment thread test/workerd/tests.mjs
import assert from "node:assert";
import process from "node:process";

globalThis.process = process;

Copy link
Copy Markdown
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
Comment thread test/workerd/main.mjs
Comment thread test/workerd/tests.mjs
},
};

// ---- node:url ----

Copy link
Copy Markdown
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.

@pi0

pi0 commented Oct 7, 2024

Copy link
Copy Markdown
Member Author

merging to iterate..

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