index.test.ts
This file
Path
src/helper/dev/index.test.ts
181 lines 路 5.2 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182import { getRouterName, inspectRoutes, showRoutes } from '.'import { Hono } from '../../hono'import { RegExpRouter } from '../../router/reg-exp-router'import type { Handler, MiddlewareHandler } from '../../types'
const namedMiddleware: MiddlewareHandler = (_, next) => next()const namedHandler: Handler = (c) => c.text('hi')const app = new Hono() .use('*', (_c, next) => next()) .get( '/', (_, next) => next(), (c) => c.text('hi') ) .get('/named', namedMiddleware, namedHandler) .post('/', (c) => c.text('hi')) .put('/', (c) => c.text('hi')) .patch('/', (c) => c.text('hi')) .delete('/', (c) => c.text('hi')) .options('/', (c) => c.text('hi')) .get('/static', () => new Response('hi'))
describe('inspectRoutes()', () => { it('should return correct data', async () => { expect(inspectRoutes(app)).toEqual([ { path: '/*', method: 'ALL', name: '[middleware]', isMiddleware: true }, { path: '/', method: 'GET', name: '[middleware]', isMiddleware: true }, { path: '/', method: 'GET', name: '[handler]', isMiddleware: false }, { path: '/named', method: 'GET', name: 'namedMiddleware', isMiddleware: true }, { path: '/named', method: 'GET', name: 'namedHandler', isMiddleware: false }, { path: '/', method: 'POST', name: '[handler]', isMiddleware: false }, { path: '/', method: 'PUT', name: '[handler]', isMiddleware: false }, { path: '/', method: 'PATCH', name: '[handler]', isMiddleware: false }, { path: '/', method: 'DELETE', name: '[handler]', isMiddleware: false }, { path: '/', method: 'OPTIONS', name: '[handler]', isMiddleware: false }, { path: '/static', method: 'GET', name: '[handler]', isMiddleware: false }, ]) })
it('should return [handler] also for sub app', async () => { const subApp = new Hono()
subApp.get('/', (c) => c.json(0)) subApp.onError((_, c) => c.json(0))
const mainApp = new Hono() mainApp.route('/', subApp) expect(inspectRoutes(mainApp)).toEqual([ { isMiddleware: false, method: 'GET', name: '[handler]', path: '/', }, ]) })})
describe('showRoutes()', () => { let logs: string[] = []
let originalLog: typeof console.log beforeAll(() => { originalLog = console.log console.log = (...args) => logs.push(...args) }) afterAll(() => { console.log = originalLog })
beforeEach(() => { logs = [] }) it('should render simple output', async () => { showRoutes(app) expect(logs).toEqual([ '\x1b[32mGET\x1b[0m /', '\x1b[32mGET\x1b[0m /named', '\x1b[32mPOST\x1b[0m /', '\x1b[32mPUT\x1b[0m /', '\x1b[32mPATCH\x1b[0m /', '\x1b[32mDELETE\x1b[0m /', '\x1b[32mOPTIONS\x1b[0m /', '\x1b[32mGET\x1b[0m /static', ]) })
it('should render output includes handlers and middlewares', async () => { showRoutes(app, { verbose: true }) expect(logs).toEqual([ '\x1b[32mALL\x1b[0m /*', ' [middleware]', '\x1b[32mGET\x1b[0m /', ' [middleware]', ' [handler]', '\x1b[32mGET\x1b[0m /named', ' namedMiddleware', ' namedHandler', '\x1b[32mPOST\x1b[0m /', ' [handler]', '\x1b[32mPUT\x1b[0m /', ' [handler]', '\x1b[32mPATCH\x1b[0m /', ' [handler]', '\x1b[32mDELETE\x1b[0m /', ' [handler]', '\x1b[32mOPTIONS\x1b[0m /', ' [handler]', '\x1b[32mGET\x1b[0m /static', ' [handler]', ]) })
it('should render not colorized output', async () => { showRoutes(app, { colorize: false }) expect(logs).toEqual([ 'GET /', 'GET /named', 'POST /', 'PUT /', 'PATCH /', 'DELETE /', 'OPTIONS /', 'GET /static', ]) })})
describe('showRoutes() in NO_COLOR', () => { let logs: string[] = []
let originalLog: typeof console.log beforeAll(() => { vi.stubEnv('NO_COLOR', '1') originalLog = console.log console.log = (...args) => logs.push(...args) }) afterAll(() => { vi.unstubAllEnvs() console.log = originalLog })
beforeEach(() => { logs = [] }) it('should render not colorized output', async () => { showRoutes(app) expect(logs).toEqual([ 'GET /', 'GET /named', 'POST /', 'PUT /', 'PATCH /', 'DELETE /', 'OPTIONS /', 'GET /static', ]) }) it('should render colorized output if colorize: true', async () => { showRoutes(app, { colorize: true }) expect(logs).toEqual([ '\x1b[32mGET\x1b[0m /', '\x1b[32mGET\x1b[0m /named', '\x1b[32mPOST\x1b[0m /', '\x1b[32mPUT\x1b[0m /', '\x1b[32mPATCH\x1b[0m /', '\x1b[32mDELETE\x1b[0m /', '\x1b[32mOPTIONS\x1b[0m /', '\x1b[32mGET\x1b[0m /static', ]) })})
describe('getRouterName()', () => { it('Should return the correct router name', async () => { const app = new Hono({ router: new RegExpRouter(), }) expect(getRouterName(app)).toBe('RegExpRouter') })})