Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 1why, one step back
1import { getRouterName, inspectRoutes, showRoutes } from '.'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 2–181why, one step back
2import { Hono } from '../../hono'
3import { RegExpRouter } from '../../router/reg-exp-router'4import type { Handler, MiddlewareHandler } from '../../types'56const namedMiddleware: MiddlewareHandler = (_, next) => next()7const namedHandler: Handler = (c) => c.text('hi')8const app = new Hono()9 .use('*', (_c, next) => next())10 .get(11 '/',12 (_, next) => next(),13 (c) => c.text('hi')14 )15 .get('/named', namedMiddleware, namedHandler)16 .post('/', (c) => c.text('hi'))17 .put('/', (c) => c.text('hi'))18 .patch('/', (c) => c.text('hi'))19 .delete('/', (c) => c.text('hi'))20 .options('/', (c) => c.text('hi'))21 .get('/static', () => new Response('hi'))2223describe('inspectRoutes()', () => {24 it('should return correct data', async () => {25 expect(inspectRoutes(app)).toEqual([26 { path: '/*', method: 'ALL', name: '[middleware]', isMiddleware: true },27 { path: '/', method: 'GET', name: '[middleware]', isMiddleware: true },28 { path: '/', method: 'GET', name: '[handler]', isMiddleware: false },29 { path: '/named', method: 'GET', name: 'namedMiddleware', isMiddleware: true },30 { path: '/named', method: 'GET', name: 'namedHandler', isMiddleware: false },31 { path: '/', method: 'POST', name: '[handler]', isMiddleware: false },32 { path: '/', method: 'PUT', name: '[handler]', isMiddleware: false },33 { path: '/', method: 'PATCH', name: '[handler]', isMiddleware: false },34 { path: '/', method: 'DELETE', name: '[handler]', isMiddleware: false },35 { path: '/', method: 'OPTIONS', name: '[handler]', isMiddleware: false },36 { path: '/static', method: 'GET', name: '[handler]', isMiddleware: false },37 ])38 })3940 it('should return [handler] also for sub app', async () => {41 const subApp = new Hono()4243 subApp.get('/', (c) => c.json(0))44 subApp.onError((_, c) => c.json(0))4546 const mainApp = new Hono()47 mainApp.route('/', subApp)48 expect(inspectRoutes(mainApp)).toEqual([49 {50 isMiddleware: false,51 method: 'GET',52 name: '[handler]',53 path: '/',54 },55 ])56 })57})5859describe('showRoutes()', () => {60 let logs: string[] = []6162 let originalLog: typeof console.log63 beforeAll(() => {64 originalLog = console.log65 console.log = (...args) => logs.push(...args)66 })67 afterAll(() => {68 console.log = originalLog69 })7071 beforeEach(() => {72 logs = []73 })74 it('should render simple output', async () => {75 showRoutes(app)76 expect(logs).toEqual([77 '\x1b[32mGET\x1b[0m /',78 '\x1b[32mGET\x1b[0m /named',79 '\x1b[32mPOST\x1b[0m /',80 '\x1b[32mPUT\x1b[0m /',81 '\x1b[32mPATCH\x1b[0m /',82 '\x1b[32mDELETE\x1b[0m /',83 '\x1b[32mOPTIONS\x1b[0m /',84 '\x1b[32mGET\x1b[0m /static',85 ])86 })8788 it('should render output includes handlers and middlewares', async () => {89 showRoutes(app, { verbose: true })90 expect(logs).toEqual([91 '\x1b[32mALL\x1b[0m /*',92 ' [middleware]',93 '\x1b[32mGET\x1b[0m /',94 ' [middleware]',95 ' [handler]',96 '\x1b[32mGET\x1b[0m /named',97 ' namedMiddleware',98 ' namedHandler',99 '\x1b[32mPOST\x1b[0m /',100 ' [handler]',101 '\x1b[32mPUT\x1b[0m /',102 ' [handler]',103 '\x1b[32mPATCH\x1b[0m /',104 ' [handler]',105 '\x1b[32mDELETE\x1b[0m /',106 ' [handler]',107 '\x1b[32mOPTIONS\x1b[0m /',108 ' [handler]',109 '\x1b[32mGET\x1b[0m /static',110 ' [handler]',111 ])112 })113114 it('should render not colorized output', async () => {115 showRoutes(app, { colorize: false })116 expect(logs).toEqual([117 'GET /',118 'GET /named',119 'POST /',120 'PUT /',121 'PATCH /',122 'DELETE /',123 'OPTIONS /',124 'GET /static',125 ])126 })127})128129describe('showRoutes() in NO_COLOR', () => {130 let logs: string[] = []131132 let originalLog: typeof console.log133 beforeAll(() => {134 vi.stubEnv('NO_COLOR', '1')135 originalLog = console.log136 console.log = (...args) => logs.push(...args)137 })138 afterAll(() => {139 vi.unstubAllEnvs()140 console.log = originalLog141 })142143 beforeEach(() => {144 logs = []145 })146 it('should render not colorized output', async () => {147 showRoutes(app)148 expect(logs).toEqual([149 'GET /',150 'GET /named',151 'POST /',152 'PUT /',153 'PATCH /',154 'DELETE /',155 'OPTIONS /',156 'GET /static',157 ])158 })159 it('should render colorized output if colorize: true', async () => {160 showRoutes(app, { colorize: true })161 expect(logs).toEqual([162 '\x1b[32mGET\x1b[0m /',163 '\x1b[32mGET\x1b[0m /named',164 '\x1b[32mPOST\x1b[0m /',165 '\x1b[32mPUT\x1b[0m /',166 '\x1b[32mPATCH\x1b[0m /',167 '\x1b[32mDELETE\x1b[0m /',168 '\x1b[32mOPTIONS\x1b[0m /',169 '\x1b[32mGET\x1b[0m /static',170 ])171 })172})173174describe('getRouterName()', () => {175 it('Should return the correct router name', async () => {176 const app = new Hono({177 router: new RegExpRouter(),178 })179 expect(getRouterName(app)).toBe('RegExpRouter')180 })181})