Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtins/web/abort/abort-signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool AbortSignal::throwIfAborted(JSContext *cx, unsigned argc, JS::Value *vp) {
if (is_aborted(self)) {
RootedValue reason(cx, JS::GetReservedSlot(self, std::to_underlying(Slots::Reason)));
JS_SetPendingException(cx, reason);
return false;
}

return true;
Expand Down
61 changes: 61 additions & 0 deletions tests/integration/abort/abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { serveTest } from "../test-server.js";
import { strictEqual, throws } from "../../assert.js";

export const handler = serveTest(async (t) => {
await t.test("throwIfAborted-throws-when-aborted", () => {
const controller = new AbortController();
const reason = new Error("boom");
controller.abort(reason);

strictEqual(controller.signal.aborted, true, "signal should be aborted");

let thrown;
try {
controller.signal.throwIfAborted();
} catch (e) {
thrown = e;
}

strictEqual(thrown, reason, "throwIfAborted must throw the abort reason");
});

await t.test("throwIfAborted-is-noop-when-not-aborted", () => {
const controller = new AbortController();
strictEqual(controller.signal.aborted, false, "signal should not be aborted");
// Must not throw when the signal is not aborted.
let threw = false;
try {
controller.signal.throwIfAborted();
} catch {
threw = true;
}
strictEqual(threw, false, "throwIfAborted must not throw when not aborted");
});

await t.test("throwIfAborted-throws-default-AbortError", () => {
const controller = new AbortController();
controller.abort(); // no reason -> default AbortError DOMException

throws(() => controller.signal.throwIfAborted());

let name;
try {
controller.signal.throwIfAborted();
} catch (e) {
name = e.name;
}
strictEqual(name, "AbortError", "default abort reason is an AbortError");
});

await t.test("AbortSignal.abort-throwIfAborted", () => {
const reason = new Error("static-abort");
const signal = AbortSignal.abort(reason);
let thrown;
try {
signal.throwIfAborted();
} catch (e) {
thrown = e;
}
strictEqual(thrown, reason, "AbortSignal.abort(reason).throwIfAborted() throws reason");
});
});
1 change: 1 addition & 0 deletions tests/integration/handlers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { handler as abort } from './abort/abort.js';
export { handler as blob } from './blob/blob.js';
export { handler as btoa } from './btoa/btoa.js';
export { handler as performance } from './performance/performance.js';
Expand Down
1 change: 1 addition & 0 deletions tests/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test_e2e(no-init-location)
test_e2e(init-location)

integration_tests(
abort
blob
btoa
crypto
Expand Down
Loading