Skip to content

Commit c78e8f7

Browse files
committed
Test throws in async handlers and many async handlers for write
1 parent 66f1d0d commit c78e8f7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/asyncify.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ async function wrap(rewriter, fn, ...args) {
9292
assertNoneState();
9393
let result = fn(...args);
9494

95-
// TODO: add test for this, if write triggers multiple async handlers?
9695
while (wasm.asyncify_get_state() === State.UNWINDING) {
9796
wasm.asyncify_stop_unwind();
9897

test/misc.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,18 @@ test("rethrows error thrown in handler", async (t) => {
7979
await t.throwsAsync(promise, { message: "Whoops!" });
8080
});
8181

82-
test("handles concurrent rewriters with async handlers", async (t) => {
82+
test("rethrows error thrown in async handler", async (t) => {
83+
const rewriter = new RawHTMLRewriter(() => {}).on("p", {
84+
async element() {
85+
throw new Error("Whoops!");
86+
},
87+
});
88+
89+
const promise = rewriter.write(new TextEncoder().encode("<p>test</p>"));
90+
await t.throwsAsync(promise, { message: "Whoops!" });
91+
});
92+
93+
test.serial("handles concurrent rewriters with async handlers", async (t) => {
8394
// Note this test requires the "safe" HTMLRewriter, see comments in
8495
// src/modules/rewriter.ts for more details
8596
const rewriter = (i: number) =>
@@ -103,6 +114,24 @@ test("handles concurrent rewriters with async handlers", async (t) => {
103114
t.deepEqual(texts, ["<p>new 3</p>", "<p>new 4</p>"]);
104115
});
105116

117+
test.serial("handles many async handlers for single chunk write", async (t) => {
118+
const rewriter = new HTMLRewriter();
119+
rewriter.on("h1", {
120+
async element(element) {
121+
await wait(50);
122+
element.setInnerContent("new h1");
123+
},
124+
});
125+
rewriter.on("p", {
126+
async element(element) {
127+
await wait(50);
128+
element.setInnerContent("new p");
129+
},
130+
});
131+
const res = await rewriter.transform("<h1>old h1</h1><p>old p</p>");
132+
t.is(res, "<h1>new h1</h1><p>new p</p>");
133+
});
134+
106135
test("rewriter allows chaining", (t) => {
107136
const rewriter = new RawHTMLRewriter(() => {});
108137
t.is(rewriter.on("p", {}), rewriter);

0 commit comments

Comments
 (0)