KScript docs
EN

Testing

Settings in kscript.json

JSON
{
  "packages": ["KScript.IO"],            // imported by every script, including tests
  "test": {
    "include": ["tests/**/*.test.kscript"],  // default: **/*.test.kscript and **/*.test.ks.xml
    "exclude": ["tests/slow"],              // default: _templates, node_modules, .git
    "timeout": 10,                          // seconds per test; 0 for no limit (default 10)
    "packages": ["KScript.Terminal"],       // imported by test files only
    "env": "test",                          // the environment tests run in (KSCRIPT_ENV)
    "database": "memory"                    // KScript.Data: a database in memory per test file, or config
  }
}

timeout: … on a test wins over --timeout, which wins over test.timeout.

test.env is the environment the tests run in, so kscript.test.json is laid over kscript.json's settings (see Settings). The API and fullstack project templates use it to give tests an in-memory database.

kscript test --env staging and a KSCRIPT_ENV that is already set win over test.env.

Tests that use KScript.Data get a database of their own in memory whatever the config says, unless test.database is config. Each test's changes are undone when it ends. This includes users backed by a model in http.auth.users. A route a test calls with @api() uses the same database, in the test's transaction: the route sees the records the test made, the test sees what the route saved, and both are undone together.

Each test file also gets temporary app folders for file-backed auth, including configured http.auth.folder stores, and for @storage(). Commands and local @api() calls share those folders within the file. The runner removes them when the file finishes, including on failure. Use a fixture inside the test file or a setup hook when tests need seeded users or files.