Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Add Context section #416

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

JoaquimLey
Copy link

@JoaquimLey JoaquimLey commented Jun 25, 2024

Also adds an example with custom routing.

Changes available at:

/docs/guides/testing#context
Screenshot 2024-06-29 at 11 11 33

ref: honojs/hono#3026

Also adds an example with custom routing.
@JoaquimLey JoaquimLey marked this pull request as ready for review June 29, 2024 10:12
@yusukebe
Copy link
Member

yusukebe commented Jun 30, 2024

Hi @JoaquimLey thank you for the PR. I'll comment here.

To set values into your context c.set() for testing, create a custom testing middleware and add it before your test(s) run.

Regarding this, there is not only a case in which we have to set it before each test. If it does not have to initialize something, we can write it without beforeEach.

const app = new Hono<{
  Variables: {
    user: typeof mockUser
  }
}>()

app.use('*', async (c, next) => {
  c.set('user', mockUser)
  await next()
})

const getFriend = vi.fn()

app.get('/user', (c) => {
  const user = c.get('user')
  getFriend(user.id)
  return c.text('/user')
})

describe('Basic', () => {
  it('should return 200 response', async () => {
    const res = await app.request('/user')
    expect(res.status).toBe(200)
    expect(getFriend).toHaveBeenCalledWith(mockUser.id)
  })
})

The problem we have to say is the below case. This test will throw the error Error: Can not add a route since the matcher is already built..

import { Hono } from 'hono'

const adminApp = new Hono()
adminApp.get('/', (c) => c.text('Admin Top'))

const app = new Hono()

beforeEach(() => {
  app.route('/admin', adminApp)
})

describe('Admin route', () => {
  it('should return 200 response', async () => {
    const res = await app.request('/admin')
    expect(res.status).toBe(200)
  })
  it('should return 404 response', async () => {
    const res = await app.request('/admin/foo')
    expect(res.status).toBe(404)
  })
})

To prevent that, we have to write the beforeEach:

let app: Hono

beforeEach(() => {
  app = new Hono()
  app.route('/admin', adminApp)
})

I think what we have to add to the document is this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants