comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Move from Karma to Playwright?

Open dfbaskin opened this issue 1 year ago • 1 comments

Since Karma is deprecated, does it make sense to move the tests over to Playwright?

As an example, this is what the tests/two-way-iframe.comlink.test.js test might look like:

import { test } from "@playwright/test";

test.describe("Comlink across iframes", function () {
  test.beforeEach(async ({ page }) => {
    await page.goto("/");
    await page.evaluate(async () => {
      this.ifr = document.createElement("iframe");
      this.ifr.sandbox.add("allow-scripts", "allow-same-origin");
      this.ifr.src = "/tests/fixtures/two-way-iframe.html";
      document.body.appendChild(this.ifr);
      await new Promise((resolve) => (this.ifr.onload = resolve));
    });
  });

  test.afterEach(async ({ page }) => {
    await page.evaluate(() => {
      this.ifr.remove();
    });
  });

  test("can communicate both ways", async ({ page }) => {
    const { value, called } = await page.evaluate(async () => {
      const Comlink = await import("/dist/esm/comlink.mjs");
      let called = false;
      const iframe = Comlink.windowEndpoint(this.ifr.contentWindow);
      Comlink.expose((a) => {
        called = true;
        return ++a;
      }, iframe);
      const proxy = Comlink.wrap(iframe);
      const value = await proxy(1, 3);
      return {
        value,
        called,
      };
    });
    test.expect(value).toEqual(5);
    test.expect(called).toEqual(true);
  });
});

If this move makes sense, I could submit a PR.

dfbaskin avatar Sep 30 '23 16:09 dfbaskin

Would love to see this, if you're open to filing a PR!

benjamind avatar Dec 14 '23 17:12 benjamind