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 ¶
- func Get[T any](c *Component, name string) T
- type Component
- func (c *Component) Call(name string, args ...any) (json.RawMessage, error)
- func (c *Component) Destroy()
- func (c *Component) Events() []Event
- func (c *Component) Eventually(cond func() bool)
- func (c *Component) Set(name string, v any) error
- func (c *Component) SetProps(props any) error
- func (c *Component) State(name string) json.RawMessage
- type Event
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Component ¶
type Component struct {
// contains filtered or unexported fields
}
Component is a mounted live component.
func Mount ¶
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 ¶
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) Eventually ¶
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.
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 ¶
WithLocals sets the request values the component reads with Locals, such as the signed-in user.
func WithRestore ¶
WithRestore mounts the component as if the page reconnected showing these values, for State declared with Bind or Restore.