Testing - ElysiaJS | ElysiaJS

ID: 2129https://elysiajs.com/patterns/unit-test.html
Source

Unit Test

Being WinterCG compliant, you can use the Request / Response classes to test an Elysia server.

Elysia provides the Elysia.handle method, which accepts a Web Standard Request and returns a Response, simulating an HTTP request.

Bun includes a built‑in test runner that offers a Jest‑like API through the bun:test module, facilitating the creation of unit tests.

Create a file test/index.test.ts in the root of your project with the following contents:

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

describe('Elysia', () => {
  it('returns a response', async () => {
    const app = new Elysia()
      .get('/', () => 'hi')

    const response = await app
      .handle(new Request('http://localhost/'))
      .then(res => res.text())

    expect(response).toBe('hi')
  })
})

Run the test with:

bun test

Note:
New requests to an Elysia server must be a fully valid URL – not just a path.

http://localhost/user   ✅
/user                    ❌

You can also use other testing libraries such as Jest to create Elysia unit tests.

Eden Treaty test

You may use Eden Treaty to create an end‑to‑end type safety test for an Elysia server as follows:

// 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, error } = await api.hello.get()

    expect(data).toBe('hi')
  })
})

See the Eden Treaty Unit Test page for setup and more information.