Learn / Errors and failures

Testing failures

A failure path is a path like any other: drive it, and assert what the reader sees.

  • Without a browser, Trace.run from foldkit-remote/testing runs the real update and Subscriptions and holds every request; each Respond takes { fail } to answer with an error. Trace.regressions names every step where a read that showed a value stopped showing it.

    ts
    import { RemoteReadError } from 'foldkit-remote'
    import { Respond, Trace } from 'foldkit-remote/testing'
    
    const trace = await Trace.run(Data, {
      start: loaded, // a Model showing the project
      update,
      active: actives,
      client: server, // a fake RemoteClient, or the server's in-process one
      steps: [
        Message.ClickedTryAgain(),
        Respond.read(Project, 'p1', { fail: new RemoteReadError({ message: 'The server is down' }) }),
      ],
    })
    expect(Trace.regressions(trace, { project: Data.get(ProjectName, 'p1') })).toEqual([])
  • A refusal made on the client is held to the server's by Law.check (foldkit-remote/testing): it runs one declared write through Data.mutate and through the server, and compares what the client predicted, a refusal included, with what the server then holds. See Testing that a prediction is the truth.

  • Diagnostics are asserted with Diagnostics.collect(): provide its report to the Effect under test (here program), then assert on reported, [] included.

    ts
    const { report, reported } = Diagnostics.collect()
    await Effect.runPromise(Effect.provideService(program, Diagnostics.Reporter, report))
    expect(reported).toEqual([])
  • On a running page, foldkit-plus/testing/playwright's Behaviors watches the page as drawn:

    ts
    import { Behaviors } from 'foldkit-plus/testing/playwright'
    
    const errors = Behaviors.consoleErrors(page) // console.error and uncaught exceptions, from now on
    await Behaviors.neverShows(page, {
      text: 'Could not load',
      during: async () => {
        await page.reload()
        await page.getByRole('heading').waitFor()
      },
    })
    expect(errors()).toEqual([])

    consoleErrors lists every error diagnostic the default reporter logs, so this test also fails on a MountFailed or a PageContained. A warning is logged with console.log and is not listed: to fail on warnings too, give the page a reporter that calls console.error for them.

Where to read more