livetest

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package livetest runs live components in Go tests, without a browser:

func TestCounter(t *testing.T) {
	c := livetest.Mount(t, "src/lib/Counter.svelte", map[string]any{"start": 2})
	if _, err := c.Call("increment"); err != nil {
		t.Fatal(err)
	}
	if got := livetest.Get[int](c, "count"); got != 3 {
		t.Errorf("count = %d", got)
	}
}

Write the test in the app's main package, next to tern.gen.go, which registers the app's components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](c *Component, name string) T

Get decodes the value the page shows for name into T.

Types

type Component

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

Component is a mounted live component.

func Mount

func Mount(t testing.TB, component string, props any, opts ...Option) *Component

Mount mounts a component, named by its .svelte path relative to the app root (or its ID), with props, and destroys it when the test ends. A panic in the component fails the test.

func (*Component) Call

func (c *Component) Call(name string, args ...any) (json.RawMessage, error)

Call runs a Func with args and returns its JSON result. By the time it returns, the state the call changed is visible to State and Get.

func (*Component) Destroy

func (c *Component) Destroy()

Destroy destroys the component now, running its OnDestroy functions.

func (*Component) Events

func (c *Component) Events() []Event

Events returns the events the component emitted so far.

func (*Component) Eventually

func (c *Component) Eventually(cond func() bool)

Eventually waits up to a second for cond, for changes that arrive from other goroutines, such as Every, Go or another component's Global. It fails the test on timeout.

func (*Component) Set

func (c *Component) Set(name string, v any) error

Set sets a State declared with Bind, as bind:value does.

func (*Component) SetProps

func (c *Component) SetProps(props any) error

SetProps passes new props, as a parent re-rendering does.

func (*Component) State

func (c *Component) State(name string) json.RawMessage

State returns the JSON value the page shows for name, or nil.

type Event

type Event struct {
	Name  string
	Value json.RawMessage
}

Event is a value a component passed to Emit.

type Option

type Option func(*goscript.LiveOptions)

Option configures Mount.

func WithLocals

func WithLocals(locals map[string]any) Option

WithLocals sets the request values the component reads with Locals, such as the signed-in user.

func WithRestore

func WithRestore(values map[string]any) Option

WithRestore mounts the component as if the page reconnected showing these values, for State declared with Bind or Restore.

Jump to

Keyboard shortcuts

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