test(types): Use `ExpectTypeOf` from vitest to test types (#1458)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #1429
authored by Rivaldi Putra · committed by GitHub · parent d3a4f42 · Browse files · 2 files·+22 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+22 added−20 removed
- tests:+22 added−20 removed
Provenance
- Commit
e4feba7- Tree
40e411c- Author
Rivaldi Putra <36479850+vadhe@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
d3a4f42
2 files changed+22 added−20 removed
modified:src/hono.test.ts+10 added-8 removed
1 /* eslint-disable @typescript-eslint/no-unused-vars */2 /* eslint-disable @typescript-eslint/ban-ts-comment */3 import { vi } from 'vitest'4+import { expectTypeOf } from 'vitest'5 import type { Context } from './context'6 import { Hono } from './hono'7 import { HTTPException } from './http-exception'8 import { logger } from './middleware/logger'1972 unmodified lines1981 })1982 app.get('/', (c) => {1983 const id = c.get('id')1984 const title = c.get('title')1984- type verifyID = Expect<Equal<number, typeof id>>1985- type verifyTitle = Expect<Equal<string, typeof title>>1985+ // type verifyID = Expect<Equal<number, typeof id>>1986+ expectTypeOf(id).toEqualTypeOf<number>()1987+ // type verifyTitle = Expect<Equal<string, typeof title>>1988+ expectTypeOf(title).toEqualTypeOf<string>()1989 return c.text(`${id} is ${title}`)1990 })1991 const res = await app.request('http://localhost/')1992 expect(res.status).toBe(200)10 unmodified lines2003 const app = new Hono<{ Bindings: Bindings }>()2004 2005 it('Should get binding variables with correct types', async () => {2006 app.get('/', (c) => {2004- type verifyID = Expect<Equal<number, typeof c.env.USER_ID>>2005- type verifyName = Expect<Equal<string, typeof c.env.USER_NAME>>2007+ expectTypeOf(c.env).toEqualTypeOf<Bindings>()2008 return c.text('These are verified')2009 })2010 const res = await app.request('http://localhost/')2011 expect(res.status).toBe(200)91 unmodified lines2103 describe('Optional parameters', () => {2104 const app = new Hono()2105 app.get('/api/:version/animal/:type?', (c) => {2106 const type1 = c.req.param('type')2105- type verify = Expect<Equal<typeof type1, string | undefined>>2107+ expectTypeOf(type1).toEqualTypeOf<string | undefined>()2108 const { type, version } = c.req.param()2107- type verify2 = Expect<Equal<typeof version, string>>2108- type verify3 = Expect<Equal<typeof type, string | undefined>>2109+ expectTypeOf(version).toEqualTypeOf<string>()2110+ expectTypeOf(type).toEqualTypeOf<string | undefined>()2111 2112 return c.json({2113 type: type,2114 })554 unmodified lines2669 TOKEN: string2670 }2671 }>()2672 app.get('/', poweredBy(), async (c) => {2671- type verify = Expect<Equal<string, typeof c.env.TOKEN>>2673+ expectTypeOf(c.env.TOKEN).toEqualTypeOf<string>()2674 })2675 })2676 })modified:src/types.test.ts+12 added-12 removed
1 /* eslint-disable @typescript-eslint/ban-types */2 /* eslint-disable @typescript-eslint/no-unused-vars */3+import { expectTypeOf } from 'vitest'4 import type { Context } from './context'5 import { Hono } from './hono'6 import { poweredBy } from './middleware/powered-by'7 import type {10 unmodified lines18 ToSchema,19 UndefinedIfHavingQuestion,20 } from './types'21 import type { Expect, Equal } from './utils/types'21-22 describe('Env', () => {23 test('Env', () => {24 type E = {25 Variables: {6 unmodified lines32 const app = new Hono<E>()33 app.use('*', poweredBy())34 app.get('/', (c) => {35 const foo = c.get('foo')36- type verify = Expect<Equal<string, typeof foo>>36+ expectTypeOf(foo).toEqualTypeOf<string>()37 const FLAG = c.env.FLAG38- type verify2 = Expect<Equal<boolean, typeof FLAG>>38+ expectTypeOf(FLAG).toEqualTypeOf<boolean>()39 return c.text('foo')40 })41 })42 })24 unmodified lines67 in: { json: Payload }68 out: { json: Payload }69 }70 >71- type verify = Expect<Equal<Expected, typeof c>>71+ expectTypeOf(c).toEqualTypeOf<Expected>()72 return c.jsonT({73 message: 'Hello!',74 })75 })76 app.get(middleware, (c) => {77 const data = c.req.valid('json')78- type verify = Expect<Equal<Payload, typeof data>>78+ expectTypeOf(data).toEqualTypeOf<Payload>()79 return c.jsonT({80 message: 'Hello!',81 })82 })25 unmodified lines108 }109 test('Context and AppType', () => {110 const route = app.get('/foo', middleware, (c) => {111 type Expected = Context<Env, '/foo', { in: { json: Payload }; out: { json: Payload } }>112- type verify = Expect<Equal<Expected, typeof c>>112+ expectTypeOf(c).toEqualTypeOf<Expected>()113 return c.jsonT({114 message: 'Hello!',115 })116 })118 unmodified lines235 await next()236 }237 const route = app.on('PURGE', '/purge', middleware, (c) => {238 const data = c.req.valid('form')239- type verify = Expect<Equal<{ id: number }, typeof data>>239+ expectTypeOf(data).toEqualTypeOf<{ id: number }>()240 return c.jsonT({241 success: true,242 })243 })73 unmodified lines317 test('Env', async () => {318 const app = new Hono<E>()319 const handler: Handler<E> = (c) => {320 const foo = c.get('foo')321- type verifyEnv = Expect<Equal<number, typeof foo>>321+ expectTypeOf(foo).toEqualTypeOf<number>()322 const id = c.req.param('id')323- type verifyPath = Expect<Equal<string, typeof id>>323+ expectTypeOf(id).toEqualTypeOf<string>()324 return c.text('Hi')325 }326 app.get('/', handler)327 const res = await app.request(url)3 unmodified lines331 test('Env, Path', async () => {332 const app = new Hono<E>()333 const handler: Handler<E, '/'> = (c) => {334 const foo = c.get('foo')335- type verifyEnv = Expect<Equal<number, typeof foo>>335+ expectTypeOf(foo).toEqualTypeOf<number>()336 return c.text('Hi')337 }338 app.get('/', handler)339 9 unmodified lines349 test('Env, Path, Type', async () => {350 const app = new Hono<E>()351 const handler: Handler<E, '/', { in: { json: User }; out: { json: User } }> = (c) => {352 const foo = c.get('foo')353- type verifyEnv = Expect<Equal<number, typeof foo>>353+ expectTypeOf(foo).toEqualTypeOf<number>()354 const { name } = c.req.valid('json')355- type verifySchema = Expect<Equal<string, typeof name>>355+ expectTypeOf(name).toEqualTypeOf<string>()356 return c.text('Hi')357 }358 })359 })281 unmodified lines