Skip to content

单元测试

¥Unit Test

根据 Eden Treaty 配置单元测试,我们可以将 Elysia 实例直接传递给 Eden Treaty,以便直接与 Elysia 服务器交互,而无需发送网络请求。

¥According to Eden Treaty config and Unit Test, we may pass an Elysia instance to Eden Treaty directly to interact with Elysia server directly without sending a network request.

我们可以使用此模式创建一个单元测试,同时包含端到端类型安全和类型级测试。

¥We may use this pattern to create a unit test with end-to-end type safety and type-level test all at once.

typescript
// test/index.test.ts
import { 
describe
,
expect
,
it
} from 'bun:test'
import {
Elysia
} from 'elysia'
import {
treaty
} from '@elysiajs/eden'
const
app
= new
Elysia
().
get
('/hello', 'hi')
const
api
=
treaty
(
app
)
describe
('Elysia', () => {
it
('returns a response', async () => {
const {
data
} = await
api
.
hello
.
get
()
expect
(
data
).
toBe
('hi')
}) })

类型安全测试

¥Type safety test

要执行类型安全测试,只需在测试文件夹上运行 tsc 即可。

¥To perform a type safety test, simply run tsc on test folders.

bash
tsc --noEmit test/**/*.ts

这对于确保客户端和服务器的类型完整性非常有用,尤其是在迁移期间。

¥This is useful to ensure type integrity for both client and server, especially during migrations.