KScript 文档
ZH

测试

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: … 优先于 --timeout,后者又优先于 test.timeout。

test.env 是测试运行所处的环境,因此 kscript.test.json 会叠加在 kscript.json 的设置之上(见 设置)。API 和全栈 项目模板 使用它为测试提供内存数据库。

kscript test --env staging 以及已设置的 KSCRIPT_ENV 优先于 test.env。

使用 KScript.Data 的测试无论配置如何都会在内存中获得各自的数据库,除非 test.database 为 config。每个测试的更改在结束时撤销。这也包括由 http.auth.users 中的模型支持的用户。测试使用 @api() 调用的路由使用同一数据库,并处于该测试的事务中:路由能看到测试所创建的记录,测试也能看到路由保存的内容,两者会一同撤销。

每个测试文件还会为基于文件的认证(包括已配置的 http.auth.folder 存储)以及 @storage() 创建临时应用文件夹。同一文件内的命令和本地 @api() 调用共享这些文件夹。文件结束时(包括失败时)运行器会将其删除。当测试需要预置的用户或文件时,请在测试文件内使用 fixture 或 setup 钩子。

Edit this page on GitHub