KScript docs
EN

Testing

Checks

expect takes the value to check first, without a name, and one check: expect($total, equals: 3). Every check's value is an expression, so text needs quotes: equals: 'hello'.

Check Passes when
expect($x, equals: 5) The values are equal, as == compares them: '5' equals 5. Lists and maps are compared item by item, and a failure says where they differ.
not_equals: 5 They aren't equal.
same: 5 They are strictly equal (===): same type and value.
contains: 'Joan' A list has the item, text has the text, or a map has the key.
not_contains: 'Joan' The opposite.
matches: '^\d+$' Text matches the regular expression.
type: list The value is a string, number, bool, list, map, date, fn or null.
count: 3 A list or map has that many items, or text has that many characters.
count_at_most: 4 A list or map has at most that many items, or text has at most that many characters. With @queries(), it checks how many queries a test sent.
close_to: 0.3 A number is within within: of this (0.000000001 unless given): expect(0.1 + 0.2, close_to: 0.3), expect($average, close_to: 4.33, within: 0.01). Use it for money, statistics and other maths that floats can't hold exactly.
has: { status: 'paid' } A map has these keys with these values, and may have others. A nested map is checked the same way. For a list, every item must have them.

message: … on any check replaces the usual message.

has: suits an API answer, where a test cares about a few fields:

<let name="order" value="{ id: 7, status: 'paid', total: 30, customer: { name: 'Ann', email: 'ann@example.com' } }" />
<expect value="$order" has="{ status: 'paid', customer: { name: 'Ann' } }" />
<expect value="[{ id: 1, active: true }, { id: 2, active: true }]" has="{ active: true }" />
$order = { id: 7, status: 'paid', total: 30, customer: { name: 'Ann', email: 'ann@example.com' } }
expect($order, has: { status: 'paid', customer: { name: 'Ann' } })
expect([{ id: 1, active: true }, { id: 2, active: true }], has: { active: true })

@assert(condition, message?) checks a condition, on a line of its own or inside an expression such as a lambda. When a comparison fails it shows the value of each side:

Text
Expected $order.total == 30 to be true.
  $order.total is 25

@fail(message?) fails the test straight away.

Errors

expect_error { } fails unless its body stops with an error. type checks the error type (an error type that extends it counts too), contains checks the message, and as keeps the error (type, message, line, file, status, cause and its fields) for more checks:

<expect_error type="Validation" contains="positive" as="err">
  <do>@set_age(-1)</do>
</expect_error>
<expect value="$err.line" type="number" />
expect_error(type: Validation, contains: 'positive', as: err) {
  @set_age(-1)
}
expect($err.line, type: number)

For one expression, throws is shorter: it passes when working out the value stops with an error of that type.

<error name="PersonNotFound" extends="NotFound" />

<fn name="find_person" params="id: int">
  <throw type="PersonNotFound">No person with id {$id}</throw>
</fn>

<expect value="@find_person(99)" throws="PersonNotFound" />
<expect value="'nope'->convert(from: json)" throws="Format" /> <!-- Json extends Format -->
error PersonNotFound extends NotFound

fn find_person(id: int) {
  throw PersonNotFound "No person with id {$id}"
}

expect(@find_person(99), throws: PersonNotFound)
expect('nope'->convert(from: json), throws: Format) // Json extends Format

A failed check inside the body is still reported as a failure, not taken as the expected error.

Output and input

capture(into: out) { } runs its body and puts what it printed in $out (lines end in \n, without the last one). input gives prompts such as input their answers, as text or a list of lines:

<capture into="out" input="['Joan', 'yes']">
  <do>@sign_up()</do>
</capture>
<expect value="$out" contains="'Welcome, Joan'" />
capture(into: out, input: ['Joan', 'yes']) {
  @sign_up()
}
expect($out, contains: 'Welcome, Joan')

Whole scripts

@run_script(path, { args, input }) runs another script on its own, as kscript path args… would, and returns { output, code, error }: what it printed, its exit code (3 when it stopped with an error), and the error or null. The path is relative to the test file. The script gets the project's packages, but not KScript.Test.

<test name="hello.kscript greets the name it is given">
  <let name="run" value="@run_script('../scripts/hello.kscript', options: { args: ['Ellie'] })" />
  <expect value="$run.code" equals="0" />
  <expect value="$run.output" equals="'Hello, Ellie!'" />
</test>
test(name: 'hello.kscript greets the name it is given') {
  $run = @run_script('../scripts/hello.kscript', options: { args: ['Ellie'] })
  expect($run.code, equals: 0)
  expect($run.output, equals: 'Hello, Ellie!')
}

Use it for scripts that do their work at the top level or call exit. A test that calls exit itself fails.

@check_script(path) checks another script as kscript check does, without running it, and returns its problems in the order they appear: a list of { line, column, severity, code, message }. Use it to say which mistakes the editor and kscript check catch. Keep scripts with mistakes made on purpose in a folder that lint.exclude leaves out (see Projects), so kscript check on the project stays clean. @check_script() checks the file anyway:

<test name="a map is not a flag">
  <let name="problems" value="@check_script('mistakes/flags.kscript')" />
  <expect value="$problems" count="1" /> <!-- one mistake gets one report -->
  <expect value="$problems[0].line" equals="2" />
  <expect value="$problems[0].message" contains="'needs true or false, but this is a map'" />
</test>
test(name: 'a map is not a flag') {
  $problems = @check_script('mistakes/flags.kscript')
  expect($problems, count: 1)       // one mistake gets one report
  expect($problems[0].line, equals: 2)
  expect($problems[0].message, contains: 'needs true or false, but this is a map')
}

Pages

@visit(path) (from KScript.Web) renders a page of the project as a browser gets it, the way kscript serve answers it: the project's routes first (a view: route, or a controller that returns @view(...), with their middleware), then the file pages under serve.root in the nearest kscript.json. Its live handlers run on the server as they would in a browser, so a test can click through a page:

  • ->click(text) runs the on-click handler of the button or link with that text (or aria-label or title).
  • ->change(text) runs the on-change handler of the input whose <label> has that text, such as a checkbox.
  • ->fill(field, value) types into the input bound to that field, and ->submit(text?) runs a form's on-submit.
  • .text is the words on the page, .html its HTML, and ->classes(text) the classes of the element whose own text that is.

Each of these returns the page, so they chain. A page that isn't there is a NotFound error. Events a handler sends with @emit_event() (or emit:) reach the other blocks' on-event listeners, as they do in the browser: the block named by target:, or every block that listens.

A visit to a route keeps the cookies the app sets (a guest cookie, the session) for its clicks, and follows redirects as a browser does: .path is where it ended up. @acting_as() signs the visit in, as it does for @api(/...), and @visit(path, { headers: { Cookie: 'guest=abc' } }) sends headers of its own (bearer: sends a token):

<import package="KScript.Web" />
<import package="KScript.Auth" />

<test name="a guest is sent to sign in">
  <expect value="@visit('/account').path" equals="'/login'" />
</test>

<test name="the account page makes a token">
  <do>@acting_as(@register('ada@example.test', 'correct-horse-battery-9'))</do>
  <let name="page" value="@visit('/account')->fill('name', 'laptop')->submit('Make a token')" />
  <expect value="$page.text" contains="'Your new token is'" />
</test>
import KScript.Web
import KScript.Auth

test(name: 'a guest is sent to sign in') {
  expect(@visit('/account').path, equals: '/login')
}

test(name: 'the account page makes a token') {
  @acting_as(@register('ada@example.test', 'correct-horse-battery-9'))
  $page = @visit('/account')->fill('name', 'laptop')->submit('Make a token')
  expect($page.text, contains: 'Your new token is')
}
<import package="KScript.Web" />

<test name="adding a task clears the box">
  <let name="page" value="@visit('/tasks')->fill('draft', 'Write the docs')->submit()" />
  <expect value="$page.text" contains="'Write the docs'" />
  <expect value="$page->classes('Write the docs')" not_contains="'done'" />
</test>
import KScript.Web

test(name: 'adding a task clears the box') {
  $page = @visit('/tasks')->fill('draft', 'Write the docs')->submit()
  expect($page.text, contains: 'Write the docs')
  expect($page->classes('Write the docs'), not_contains: 'done')
}

samples/task-board has a page whose buttons are all handler shorthands, with a test for each.

Edit this page on GitHub