Sidebar

Displays the mobile sidebar.

Search the docs

Search the docs

charter
charter
What charter is
See it running
Where to go
What an app made with charter looks like
Getting started
Getting started
Working on it
Before a push
Shipping it
Tools
Every command
The commands, and what each one proves
The commands, and what each one proves
What is checked, and when you find out
What no compiler checks
create
dev
add
check
test
deploy
pack
run
The rest
An app's layout
An app's layout
What the Worker is bound to
main.go
What is generated
What is never in git
Pages
Pages
With the commands
The tree
A page declares what it needs
A region that changes by itself
The page being shown
A fragment opened as a page
What a page calls
Links
A link shows the next page with no page load
Which pages search engines get
A page that is the same for every reader
The head
The API
The API
With the commands
An operation
Who may call
Limits
SDKs
MCP
The live feed
The live feed
With the commands
Writing
Three ways out, one feed
A page's stream holds no Go
Resuming
The database
The database
With the commands
Changing it
Queries
Every statement is checked
One definition
Seeding
A deployed database
Reads near the reader
By hand
Signing in, and who may do what
Signing in, and who may do what
With the commands
One caller
Saying what is needed
Roles
The first admin, and inviting people
Signing in
Another device
An address to get back in by
Testing it
The clients
What is left to you
Languages and search engines
Languages and search engines
With the commands
Languages
Search engines
Agents
Screens and themes
Screens and themes
With the commands
The interface is gsxui's
Screens
The layout
What a page is made of
Themes
The editor, in your app
A style
While developing
Light and dark
What is checked, and what is not
Docs
Docs
With the commands
Groups, and the order
What you write, and what charter writes
Links are checked
Showing them
What comes with them
What the pages have
Code taken from a real file
Tests
Tests
A page, as plain Go
The whole app, on a real Worker
A page, in a real browser
What to test, and what not to
The API, through its own SDK
Where a test runs
A test leaves the Worker as it found it
Secrets a test needs
What else charter test does
Deploying
Deploying
Two Workers
With the commands
The plan
Nothing of your account in the repository
Credentials
Secrets
Resources
Taking an app away
Your own domain
Email
After a deploy
Environments
Everything Cloudflare gives a Worker
The admin screen
The admin screen
The pages
The app's own facts
From a program
What it does not do
Watching a deployed Worker
Watching a deployed Worker
Is it well?
Three numbers
Cloudflare's limits
Under load
A first hit
Every data centre, by country
What happened?
A failure has a reference
Memory
What charter leaves to Cloudflare
Upgrading
Upgrading
0.82
0.81
0.80
0.79
0.78
0.77
0.76
0.75
0.74
0.73
0.72
0.71
0.69
0.65
0.64
A binding, a variable, a secret's name
A scope
Screens
0.63
When upgrade stops
For your coding agent
For your coding agent
The plugins
In an app made with charter
Anywhere else
One set of commands
What an agent can ask
What a language model reads
What the Worker is bound to
What the Worker is bound to

Tests

Tests are of the pages and of the API. Two kinds: a page as plain Go, in a fraction of a second, and the whole app on a real Worker.

A page, as plain Go

A page declares what it needs, so a test hands it its own. No Worker, no database, no browser.

example/pages/pages_test.go
// The home page's data is the newest notes first.
func TestHomeProps(t *testing.T) {
	store := &notes{}
	store.Create(context.Background(), "first", "u1", "Ada")
	store.Create(context.Background(), "second", "u1", "Ada")
	props, _, _, _, _, _, _, err := Home{}.Props(httptest.NewRequest("GET", "/", nil), store, writer, page{})
	if err != nil || len(props.Notes) != 2 || props.Notes[0].Body != "second" {
		t.Fatalf("%+v %v", props, err)
	}
}

These live beside the pages (pages/pages_test.go) and run with go test, which charter test does first.

The whole app, on a real Worker

go tool charter test             # the Worker on wrangler dev
go tool charter test --deployed  # the deployed one: the same tests, and the audit

The local Worker starts on a database of its own, made new, filled by the app's seed and thrown away: a test can count on the seed's people and notes, and changes nothing you have. The deployed Worker's database is real, which is why a test leaves it as it found it (below), and why a test that needs the seed's tokens runs only locally (apptest.Local()): a deployed database never has them.

A page, in a real browser

example/test/page_home_test.go
// A note typed into one open page shows at once in another: the form, the feed, htmx's SSE swap.
func TestANoteShowsOnEveryOpenPage(t *testing.T) {
	t.Parallel()
	writer := apptest.Browser(t)
	reader := writer.Tab()
	// Writing is for whoever is signed in; reading is anyone's.
	writer.SignUp(at(screens.SignInPage{}), "Writer")
	writer.Open("/")
	reader.Open("/")
	body := "typed at " + time.Now().Format(time.RFC3339Nano)
	writer.Type(el(pages.Home.Form)+` input[name=body]`, body)
	writer.Click(el(pages.Home.Form) + ` button[type=submit]`)
	reader.WaitText(el(pages.Home.List), body)
	writer.WaitText(el(pages.Home.List), body)
	if left := writer.Attr(el(pages.Home.Form)+` input[name=body]`, "value"); left != "" {
		t.Errorf("the form still holds %q after posting", left)
	}
}

apptest.Browser is Chrome on the Worker under test, so htmx, the feed and the scripts Vite built are part of what is tested. el asks the app's own tree for a component's selector (test/tree_test.go): a renamed component breaks the test when it is compiled, not when a selector quietly matches nothing.

A browser is Chrome, and it is what the time goes on: of the example's suite, the tests that open one take nine tenths of it, about a second each, and the forty that do not take a few hundredths. So:

  • A test that opens a browser says t.Parallel() as its first line. They then run beside each other, and the suite takes a third of the time (the example's: 29 s to 11 s on a local Worker, 85 s to 22 s on the deployed one).
  • A test finds its own rows. Others write while it runs, so "the first row of the list" is not its note. It asks the tree for the row by the note's id (row(note.ID) in test/tree_test.go), and never counts what is on a page.
  • A test waits for the thing, not for a while. WaitText and its like ask until it is there. A time.Sleep that is long enough on your machine is too short on a deployed Worker that is starting.

What to test, and what not to

A test is for what running the app shows and nothing else does.

Do not testBecause
That a link, an id or a target reaches its pagethe tree and structpages-lint say so when the app is compiled and checked, and charter test follows every link
That a statement names real columns, with the right typessqlc checks it against the schema
That a binding, a variable or a scope existsit is a Go value: one that is not there does not compile
That the API's spec matches the codecharter check compares them
What charter's own screens do (the docs' pages, their search, their menu)charter tests them once, on its own site
Do testBecause
Who may do what, signed in as each role, on the page and through the APIwhether a moderator should have a scope is your decision, and only a request shows it
What a binding does: a file kept and given back, a queue's message consumed, an email sentthe name is checked; what Cloudflare does with it is not
What a reader sees happen: a list that follows a feed in a second tab, a form refused in place, a menu that opensit is behaviour in a browser
The API through its generated SDKa client is proven by calling the Worker, never only by compiling

The API, through its own SDK

note, err := as.Notes.Create(t.Context(), &notes.CreateNotesRequest{Body: body})

The client is the one in sdk/go, generated from the contract: the same one another repository would import.

Where a test runs

FolderRuns against
test/both
test/dev/a local Worker only
test/deployed/a deployed one only

A test that only one can answer asks apptest.Local().

A test leaves the Worker as it found it

The same tests ask the deployed Worker, whose database is real. So a test takes away what it makes:

  • People. page.SignUp deletes the account again when the test is done, on the account page's own form, and with it what the app keeps of that person. A test that signs up by hand calls page.Leave(path).
  • Rows. A test that writes through the API takes the row away in t.Cleanup (the example's write helper).

page.JS(expression) evaluates in the page, for what a test cannot do as a reader does: the example stands in for the device's share sheet with it, and checks what the Share button hands over.

The example holds itself to this in test/main_test.go: a local Worker starts with the seed, and when the tests are done it must have the seed's people and notes and no more, or the run fails and names what was left.

Secrets a test needs

A test that must be someone asks for a secret of the Worker by name: apptest.Secret(t, "ADMIN_TOKEN"). Locally that is the throwaway value in .dev.vars. For the deployed Worker, go tool charter test --deployed hands the tests each secret fnox has for it (see deploying); a test whose secret is missing is skipped, and charter says which.

What else charter test does

The standard test of an API with a contract, and the search-engine audit.