|
| 1 | +import path from "path"; |
| 2 | +import { describe, expect, test, beforeAll, afterAll } from "vitest"; |
| 3 | +import { unstable_dev } from "../../../packages/wrangler/wrangler-dist/cli.js"; |
| 4 | +import type { UnstableDevWorker } from "../../../packages/wrangler/wrangler-dist/cli.js"; |
| 5 | + |
| 6 | +describe("Worker", () => { |
| 7 | + let worker: UnstableDevWorker; |
| 8 | + |
| 9 | + // TODO: Remove this when `workerd` has Windows support |
| 10 | + if (process.env.RUNNER_OS === "Windows") { |
| 11 | + test("dummy windows test", () => { |
| 12 | + expect(process.env.RUNNER_OS).toStrictEqual("Windows"); |
| 13 | + }); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + worker = await unstable_dev(path.resolve(__dirname, "index.js"), { |
| 19 | + bundle: false, |
| 20 | + experimental: { experimentalLocal: true }, |
| 21 | + }); |
| 22 | + }, 30_000); |
| 23 | + |
| 24 | + afterAll(() => worker.stop()); |
| 25 | + |
| 26 | + test("module traversal results in correct response", async () => { |
| 27 | + const resp = await worker.fetch(); |
| 28 | + const text = await resp.text(); |
| 29 | + expect(text).toMatchInlineSnapshot( |
| 30 | + `"Hello Jane Smith and Hello John Smith"` |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + test("module traversal results in correct response for CommonJS", async () => { |
| 35 | + const resp = await worker.fetch("/cjs"); |
| 36 | + const text = await resp.text(); |
| 37 | + expect(text).toMatchInlineSnapshot( |
| 38 | + `"CJS: Hello Jane Smith and Hello John Smith"` |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test("correct response for CommonJS which imports ESM", async () => { |
| 43 | + const resp = await worker.fetch("/cjs-loop"); |
| 44 | + const text = await resp.text(); |
| 45 | + expect(text).toMatchInlineSnapshot('"CJS: cjs-string"'); |
| 46 | + }); |
| 47 | + |
| 48 | + test("support for dynamic imports", async () => { |
| 49 | + const resp = await worker.fetch("/dynamic"); |
| 50 | + const text = await resp.text(); |
| 51 | + expect(text).toMatchInlineSnapshot(`"dynamic"`); |
| 52 | + }); |
| 53 | + |
| 54 | + test("basic wasm support", async () => { |
| 55 | + const resp = await worker.fetch("/wasm"); |
| 56 | + const text = await resp.text(); |
| 57 | + expect(text).toMatchInlineSnapshot('"42"'); |
| 58 | + }); |
| 59 | + |
| 60 | + test("resolves wasm import paths relative to root", async () => { |
| 61 | + const resp = await worker.fetch("/wasm-nested"); |
| 62 | + const text = await resp.text(); |
| 63 | + expect(text).toMatchInlineSnapshot('"nested42"'); |
| 64 | + }); |
| 65 | + |
| 66 | + test("wasm can be imported from a dynamic import", async () => { |
| 67 | + const resp = await worker.fetch("/wasm-dynamic"); |
| 68 | + const text = await resp.text(); |
| 69 | + expect(text).toMatchInlineSnapshot('"sibling42subdirectory42"'); |
| 70 | + }); |
| 71 | + |
| 72 | + test("text data can be imported", async () => { |
| 73 | + const resp = await worker.fetch("/txt"); |
| 74 | + const text = await resp.text(); |
| 75 | + expect(text).toMatchInlineSnapshot('"TEST DATA"'); |
| 76 | + }); |
| 77 | + |
| 78 | + test("binary data can be imported", async () => { |
| 79 | + const resp = await worker.fetch("/bin"); |
| 80 | + const bin = await resp.arrayBuffer(); |
| 81 | + const expected = new Uint8Array(new ArrayBuffer(4)); |
| 82 | + expected.set([0, 1, 2, 10]); |
| 83 | + expect(new Uint8Array(bin)).toEqual(expected); |
| 84 | + }); |
| 85 | +}); |
0 commit comments