kittest

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package kittest tests Tern apps from Go: call a load, an action or an endpoint with a request made up for the test, and check what it returns; or run the whole app and query the HTML its pages render.

A load, as a signed-in user, against a database made for the test:

func TestProjects(t *testing.T) {
	db := kittest.SQLite(t, migrations.FS)
	acc := ... // accounts.New(db, …)
	app := kittest.App(t, kit.WithPlugins(acc))
	c := kittest.Ctx(t, app, kittest.Path("/projects"))
	acc.ActAs(c, user)
	data, err := projects.Load(c)
	...
}

The whole app, in a test in package main, with its generated ternApp:

func TestHome(t *testing.T) {
	app, err := ternApp()
	...
	client := kittest.NewClient(t, app)
	page := client.Get("/")
	if page.Status != 200 || page.Text("h1") != "Welcome" { ... }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func App

func App(t testing.TB, opts ...kit.Option) *kit.App

App returns an App with no routes, for calling loads, actions and endpoints with Ctx. Pass the options the real app uses, its plugins among them, so their hooks run. It is closed when the test ends.

func Ctx

func Ctx(t testing.TB, app *kit.App, opts ...Option) *kit.Ctx

Ctx returns the Ctx of a request made up of opts, with app's hooks run: what a load, action or endpoint gets. Its response is a recorder: Response returns it.

func Failed

func Failed(err error) (status int, data any, ok bool)

Failed reports whether err is an action's Fail, with its data.

func Redirected

func Redirected(err error) (status int, location string, ok bool)

Redirected reports whether err redirects, and where.

func Response

func Response(c *kit.Ctx) *httptest.ResponseRecorder

Response is the response written to a Ctx from Ctx so far, with the headers and cookies it set.

func SQLite

func SQLite(t testing.TB, migrations ...fs.FS) *sql.DB

SQLite returns an empty SQLite database in memory for this test, with the migrations in each fsys applied (see package migrate). It is closed when the test ends; every test gets its own.

func Status

func Status(err error) int

Status is the HTTP status err responds with: 200 for nil, the status of kit.Error, Redirect and Fail, and 500 for anything else.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client sends requests to an app in the test, as a browser would: it keeps cookies, sends forms from the app's own origin, and doesn't follow redirects, so tests see them.

func NewClient

func NewClient(t testing.TB, h http.Handler) *Client

NewClient serves h, usually the App from the app's ternApp, for the test. An App is started first, so its plugins' start functions run, migrations among them, and is closed when the test ends.

func (*Client) Browser added in v1.11.0

func (c *Client) Browser() *Client

Browser returns another client of the same app, with no cookies: a second user, or a visitor who isn't signed in.

func (*Client) Do

func (c *Client) Do(req *http.Request) *Page

Do sends req, from the app's own origin.

func (*Client) Get

func (c *Client) Get(path string) *Page

Get requests path as a page load.

func (*Client) PostForm

func (c *Client) PostForm(path string, values url.Values) *Page

PostForm submits values to path, as a form on the app's own pages does: action "?/name" in path for a named action.

func (*Client) URL

func (c *Client) URL(path string) string

URL is the test server's address for path.

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element is an HTML element of a Page.

func (*Element) Attr

func (e *Element) Attr(name string) string

Attr is the value of an attribute, "" if the element hasn't it.

func (*Element) Find

func (e *Element) Find(selector string) []*Element

Find returns the elements inside e matching selector.

func (*Element) Tag

func (e *Element) Tag() string

Tag is the element's name: "a", "h1".

func (*Element) Text

func (e *Element) Text() string

Text is the element's text, with spaces collapsed.

type Option

type Option func(*request)

Option describes the request of a Ctx.

func Context

func Context(ctx context.Context) Option

Context sets the request's context, for deadlines or values of your own.

func Cookie(c *http.Cookie) Option

Cookie adds a cookie to the request.

func Form

func Form(values url.Values) Option

Form sends values as a form, as an action gets them.

func Header(name, value string) Option

Header sets a request header.

func JSON

func JSON(v any) Option

JSON sends v as a JSON body, as a typed endpoint gets it.

func Local

func Local(key string, v any) Option

Local sets a value in the Ctx's Locals.

func Method

func Method(m string) Option

Method sets the request's method. The default is GET, or POST with a form or JSON body.

func NoHooks

func NoHooks() Option

NoHooks skips the App's hooks.

func Parent

func Parent(v any) Option

Parent sets the layouts' data that kit.Parent decodes.

func Path

func Path(p string) Option

Path sets the request's path and query: "/blog/hello?page=2". The default is "/".

func Route

func Route(id string, params ...string) Option

Route sets the route's ID and parameters, as the route's Load gets them: Route("/blog/slug.param", "slug", "hello").

type Page

type Page struct {
	*http.Response
	Status int
	Body   string
	// contains filtered or unexported fields
}

Page is a response, its body read, and its HTML parsed when it is HTML.

func (*Page) Find

func (p *Page) Find(selector string) []*Element

Find returns the elements matching a CSS selector. The selectors are a subset: tag names, #id, .class, [attr], [attr=value], combined (a.button[href]), with descendant (space) and child (>) combinators, and lists (h1, h2).

func (*Page) First

func (p *Page) First(selector string) *Element

First returns the first element matching selector, or nil.

func (*Page) Has

func (p *Page) Has(selector string) bool

Has reports whether an element matches selector.

func (*Page) Location

func (p *Page) Location() string

Location is where a redirect goes, "" for other responses.

func (*Page) Text

func (p *Page) Text(selector string) string

Text is the text of the first element matching selector, with spaces collapsed; "" if none does.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL