Manual Tests Fast Tests

Fast Tests

elements man tests/fast Read as markdown

A build is not done until the tests it reaches pass, and the page does not reload before then. So every save waits for the tests that import the code you changed, and a slow test slows every one of those saves.

What Reruns

A test file reruns when code it imports changes, directly or through other imports. Stylesheets and assets never rerun a test. A page's test imports the page, so it reruns on every edit to the page's template and to each service the page uses.

Sign In Through the Session

A password check is slow on purpose: crypt() with a bcrypt salt of cost 12 takes about 250 ms. A test file that calls your sign-in rpc ten times adds more than two seconds to every save that reaches it.

Log in through the session instead. It costs nothing:

import { test, equal, session, sql } from "@elements/app";
import { listMyJobs } from "./template";

test("a customer sees their own jobs", () => {
  let user = sql<{ id: string; name: string }>(`
    select id, name from users where email = 'maya@example.com'
  `).firstOrThrow();

  session.login({ userId: user.id, userName: user.name });

  equal(listMyJobs().every((j) => j.customerId === user.id), true);
});

Test the sign-in rpc once, in its own file next to the code it checks (for example app/shared/services/auth.test.ts), with one case per outcome: the right password, the wrong one, an unknown email.

Keep a Slow Test Narrow

When a test has to be slow, give it its own file that imports only the code it tests. It then reruns when that code changes, instead of on every edit to a page that happens to share the file.