Skip to content

Commit

Permalink
chore: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 3, 2024
1 parent 8de9ed5 commit e923340
Showing 1 changed file with 56 additions and 44 deletions.
100 changes: 56 additions & 44 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,77 @@ import { it, describe, expect } from "vitest";
import { evalModule } from "mlly";
import { nodeResolve as rollupNodeResolve } from "@rollup/plugin-node-resolve";
import { rollup } from "rollup";
import unwasm from "../src/plugin";
import { UnwasmPluginOptions, rollup as unwasmRollup } from "../src/plugin";

const r = (p: string) => fileURLToPath(new URL(p, import.meta.url));

await rm(r(".tmp"), { recursive: true }).catch(() => {});

describe("plugin:rollup-inline", () => {
it("works", async () => {
const build = await rollup({
input: r("fixture/static-import.mjs"),
plugins: [rollupNodeResolve({}), unwasm.rollup({})],
});
const { output } = await build.write({
format: "esm",
entryFileNames: "index.mjs",
chunkFileNames: "[name].mjs",
dir: r(".tmp/rollup-inline"),
});
describe("plugin:rollup", () => {
it("inline", async () => {
const { output } = await _rollupBuild(
r("fixture/static-import.mjs"),
"rollup-inline",
{},
);
const code = output[0].code;
const mod = await evalModule(code, { url: r("fixture/static-import.mjs") });
expect(mod.test()).toBe("OK");
});
});

describe("plugin:rollup-esm", () => {
it("works", async () => {
const build = await rollup({
input: r("fixture/dynamic-import.mjs"),
plugins: [rollupNodeResolve({}), unwasm.rollup({ esmImport: true })],
});
const { output } = await build.write({
format: "esm",
entryFileNames: "index.mjs",
chunkFileNames: "[name].mjs",
dir: r(".tmp/rollup-esm"),
});
it("esmImport", async () => {
const name = "rollup-esm";
const { output } = await _rollupBuild(
r("fixture/dynamic-import.mjs"),
name,
{ esmImport: true },
);

const code = (output[1] && "code" in output[1] && output[1].code) || "";
const esmImport = code.match(/["'](.+wasm)["']/)?.[1];
expect(esmImport).match(/\.\/wasm\/\w+-[\da-f]+\.wasm/);
expect(existsSync(r(`.tmp/rollup-esm/${esmImport}`))).toBe(true);
expect(existsSync(r(`.tmp/${name}/${esmImport}`))).toBe(true);

const { Miniflare } = await import("miniflare");
const mf = new Miniflare({
modules: true,
modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }],
scriptPath: r(".tmp/rollup-esm/_mf.mjs"),
script: `
import { test } from "./index.mjs";
export default {
async fetch(request, env, ctx) {
return new Response(test());
}
}
`,
});
const res = await mf.dispatchFetch("http://localhost");
const resText = await res.text();
const resText = await _evalCloudflare(name).then((r) => r.text());
expect(resText).toBe("OK");
await mf.dispose();
});
});

// --- Utils ---

async function _rollupBuild(
entry: string,
name: string,
pluginOpts: UnwasmPluginOptions,
) {
const build = await rollup({
input: r(entry),
plugins: [rollupNodeResolve({}), unwasmRollup(pluginOpts)],
});
return await build.write({
format: "esm",
entryFileNames: "index.mjs",
chunkFileNames: "[name].mjs",
dir: r(`.tmp/${name}`),
});
}

async function _evalCloudflare(name: string) {
const { Miniflare } = await import("miniflare");
const mf = new Miniflare({
modules: true,
modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }],
scriptPath: r(`.tmp/${name}/_mf.mjs`),
script: `
import { test } from "./index.mjs";
export default {
async fetch(request, env, ctx) {
return new Response(test());
}
}
`,
});
const res = await mf.dispatchFetch("http://localhost");
await mf.dispose();
return res;
}

0 comments on commit e923340

Please sign in to comment.