Unit test
Elysia provides a Elysia.fetch function to easily test your application.
Elysia.fetch takes a Web Standard Request, and returns a Response similar to the browser’s fetch API.
import { Elysia } from 'elysia'
const response = await app.fetch(new Request('http://localhost/'))
console.log(await response.text())
This will run a request like an actual request (not simulated).
Test
This allows us to easily test our application without running a server.
import { describe, it, expect } from 'bun:test'
import { Elysia } from 'elysia'
describe('Elysia', () => {
it('should return Hello World', async () => {
const app = new Elysia().get('/', 'Hello World')
const response = await app.fetch(new Request('http://localhost/'))
const text = await response.text()
expect(text).toBe('Hello World')
import { describe, it, expect } from 'vitest'
import { Elysia } from 'elysia'
describe('Elysia', () => {
it('should return Hello World', async () => {
const app = new Elysia().get('/', 'Hello World')
const response = await app.fetch(new Request('http://localhost/'))
const text = await response.text()
expect(text).toBe('Hello World')
import { describe, test, expect } from '@jest/globals'
import { Elysia } from 'elysia'
describe('Elysia', () => {
test('should return Hello World', async () => {
const app = new Elysia().get('/', 'Hello World')
const response = await app.fetch(new Request('http://localhost/'))
const text = await response.text()
expect(text).toBe('Hello World')
See Unit test.
Assignment
Let’s click the icon in the preview to see how’s the request is logged.