index.test.ts
This file
Path
src/middleware/context-storage/index.test.ts
43 lines · 1.1 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1import { contextStorage, getContext, tryGetContext } from '.'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 2why, one step back2import { Hono } from '../../hono'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–43why, one step back34describe('Context Storage Middleware', () => {5 type Env = {6 Variables: {7 message: string8 }9 }1011 const app = new Hono<Env>()1213 app.use(contextStorage())14 app.use(async (c, next) => {15 c.set('message', 'Hono is hot!!')16 await next()17 })18 app.get('/', (c) => {19 return c.text(getMessage())20 })21 app.get('/optional', (c) => {22 const optionalContext = tryGetContext<Env>()23 return c.text(optionalContext?.var.message ?? 'no context')24 })2526 const getMessage = () => {27 return getContext<Env>().var.message28 }2930 it('Should get context', async () => {31 const res = await app.request('/')32 expect(await res.text()).toBe('Hono is hot!!')33 })3435 it('Should return undefined when context is missing', () => {36 expect(tryGetContext<Env>()).toBeUndefined()37 })3839 it('Should get context when available via tryGetContext', async () => {40 const res = await app.request('/optional')41 expect(await res.text()).toBe('Hono is hot!!')42 })43})