Throws and Writes
elements man tests/throwsHow to test that a call is refused, and how to test a LiveTable write through a view, as a template would.
Testing That Code Throws
A bare thrown error records a Crash and stops the block. To assert that a
call should throw, pass it to throws(). It returns the error, so the test
checks its kind and message.
test.tsimport { test, throws, assert, AuthError } from "@elements/app"; test("posting while logged out is rejected", () => { let err = throws(() => createPost({ title: "hi" })); assert(err instanceof AuthError, `got ${err}`); });
A call that returns records a failure on the throws line, like a false
assert, and the test continues.
It works for SQL errors. A constraint violation or a raise in a trigger
aborts the transaction the test runs in, and every statement after it fails
with "current transaction is aborted". throws() runs the call inside a
savepoint and rolls back to it, so the test's transaction stays usable and
nothing the call wrote survives:
test.tstest("an email is unique", () => { sql(`insert into users (email) values (${"a@b.test"})`); let err = throws(() => sql(`insert into users (email) values (${"a@b.test"})`)); assert(err.message.includes("unique"), `got ${err}`); equal(sql(`select count(*)::int as n from users where email = ${"a@b.test"}`).firstOrThrow().n, 1); });
Put the error in the message. Without `got ${err}`, when the call
throws something unexpected (usually a real bug, not the rejection you were
testing for), the assertion fails with assertion failed and the actual error,
its type and its message are discarded. Interpolating err changes nothing when
the test passes, and when it fails the message shows the actual error.
Savepoints on one connection are serial, so throws() is not for use inside
a Promise.all, the same as a nested test().
Testing a LiveTable Write
insert, update, and delete work on the server, so a test calls them the
same way a template does: on a view. table.view() opens one, exactly as a
route would, and the write runs the steps the server runs for a write from the browser:
the partition check, your handler or the SQL the LiveTable generates, then the
broadcast.
test.tsimport { test, throws, assert, equal, session, sql, AuthError } from "@elements/app"; import { comments } from "./services"; test("a member can comment", () => { session.login({ userId: "u1", userName: "alice", }); comments.view().insert({ text: "hi", author: "alice", }); equal(sql(`select text from comments`).firstOrThrow().text, "hi"); });
A partition is passed here too: comments.view({ roomId }).
The session is the one the test is running in, so a handler's session check reads the same session values it would read in a request. A test that logs nobody in is an anonymous caller, which is how you test that a write is refused:
test.tstest("an anonymous visitor cannot comment", () => { let err = throws(() => comments.view().insert({ text: "nope", author: "nobody", })); assert(err instanceof AuthError, `got ${err}`); equal(sql(`select count(*) from comments where author = 'nobody'`).firstOrThrow().count, 0); });
Export the mutation so a test can import it, from the page's
template.ehtml, its services.ts, or app/shared/services/. Give it the
view as a parameter. The template passes the view the route opened, and the test
passes a view it opens itself:
services.tsexport function postComment(view: LiveView<Comment>, text: string) { view.insert({ text, author: session.getOrThrow("userName"), }); } // test.ts postComment(comments.view(), "hi");