Skip to content

Unit Test

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

Elysia provides the Elysia.handle method, which accepts a Web Standard Request and returns 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 test/index.test.ts in the root of project directory with the following:

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

describe('Elysia', () => {
    it('return 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')
    })
})

Then we can perform tests by running bun test

bash
bun test

New requests to an Elysia server must be a fully valid URL, NOT a part of a URL.

The request must provide URL as the following:

URLValid
http://localhost/user
/user

We can also use other testing libraries like Jest or testing library to create Elysia unit tests.

Eden Treaty test

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

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
('return a response', async () => {
const {
data
,
error
} = await
api
.
hello
.
get
()
expect
(
data
).
toBe
('hi')
}) })

See Eden Treaty Unit Test for setup and more information.