Skip to content

Commit

Permalink
add tests for hono adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mathysth committed Jun 17, 2024
1 parent 85583de commit e9282c8
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions packages/hono-openapi-adapter/src/start/hono-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
import { describe, expect, it } from 'bun:test';
import { beforeAll, describe, expect, it } from 'bun:test';
import { type IHttpServe, IocContainer } from '@cosmoosjs/core';
import type { FactoryConfig } from '@customTypes/index';
import { OpenAPIHono, createRoute } from '@hono/zod-openapi';
import { Server } from '@server/server';
import { ReasonPhrases, StatusCodes } from 'http-status-codes';
import z from 'zod';
import { HttpFactory } from './hono-adapter';

describe.only('HonoAdapter', () => {
const adapter = HttpFactory;
const container = IocContainer.container;
adapter.bindContainers(container);
const server = container.get(Server);

beforeAll(() => {
// Setup routes
function setErrorRoute(path: string) {
const route = createRoute({
method: 'get',
path,
responses: {
200: {
content: {
'application/json': {
schema: z.object({
message: z.string(),
}),
},
},
description: 'ok',
},
},
});
server.hono.openapi(route, async (_ctx) => {
throw new Error();
});
}

setErrorRoute('/error');
setErrorRoute('/');
});

it.only('it should get the server', async () => {
const server = container.get(Server);
expect(server).toBeTruthy();
expect(server).toBeInstanceOf(Server);
expect(server.hono).toBeInstanceOf(OpenAPIHono);
});

it.only('it should config and return an object with type IHttpServe', async () => {
const serverConfig: FactoryConfig<string> = {
const serverConfig: FactoryConfig = {
port: 3001,
metadata: {
enableSwaggerInProd: false,
Expand Down Expand Up @@ -48,4 +80,32 @@ describe.only('HonoAdapter', () => {
expect(httpServerConfig).toMatchObject(HttpServer);
expect(httpServerConfig.port).toEqual(3001);
});

it.only('it should return an error', async () => {
const res = await server.hono.request('/', {
method: 'get',
headers: {
'Content-Type': 'application/text',
},
});
const response = await res.text();
expect(res.status).toEqual(StatusCodes.INTERNAL_SERVER_ERROR);
expect(response).toEqual(ReasonPhrases.INTERNAL_SERVER_ERROR);
});

it.only('it should setup a custom error handler', async () => {
adapter.exceptionHandler((_err, ctx, _logger) => {
return ctx.text('custom error', StatusCodes.INTERNAL_SERVER_ERROR);
});

const res = await server.hono.request('/error', {
method: 'get',
headers: {
'Content-Type': 'application/text',
},
});
const response = await res.text();
expect(res.status).toEqual(StatusCodes.INTERNAL_SERVER_ERROR);
expect(response).toEqual('custom error');
});
});

0 comments on commit e9282c8

Please sign in to comment.