Skip to content
在 AI 工具中打开 Anthropic

单元测试

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

我们可以使用这种模式一次创建具有端到端类型安全和类型级别测试的单元测试。

typescript
// test/index.test.ts
import { describe, expect, it } from 'bun:test'
import { Elysia } from 'elysia'
import { treaty } from '@elysia/eden'

const app = new Elysia().get('/hello', 'hi')
const api = treaty(app)

describe('Elysia', () => {
    it('返回响应', async () => {
        const { data } = await api.hello.get()

        expect(data).toBe('hi')
              // ^?

    })
})

类型安全测试

要执行类型安全测试,只需运行 tsc 来测试文件夹。

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

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