Repository: honojs/hono
File: src/hono.test.ts (TypeScript)

Code before:

    const book = app.basePath('/book')
    book.get('/', (c) => c.text('get /book'))
    book.get('/:id', (c) => {
      return c.text('get /book/' + c.req.param('id'))
    })
    book.post('/', (c) => c.text('post /book'))

    const user = app.basePath('/user')
    user.get('/login', (c) => c.text('get /user/login'))
    user.post('/register', (c) => c.text('post /user/register'))

    const appForEachUser = user.basePath(':id')
    appForEachUser.get('/profile', (c) => c.text('get /user/' + c.req.param('id') + '/profile'))

    app.get('/add-path-after-route-call', (c) => c.text('get /add-path-after-route-call'))

    let res = await app.request('http://localhost/book', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /book')

    res = await app.request('http://localhost/book/123', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /book/123')

    res = await app.request('http://localhost/book/keys', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /book/keys')

    res = await app.request('http://localhost/book', { method: 'POST' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('post /book')

    res = await app.request('http://localhost/book/', { method: 'GET' })
    expect(res.status).toBe(404)

    res = await app.request('http://localhost/user/login', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /user/login')

    res = await app.request('http://localhost/user/register', { method: 'POST' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('post /user/register')

    res = await app.request('http://localhost/user/123/profile', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /user/123/profile')

    res = await app.request('http://localhost/add-path-after-route-call', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /add-path-after-route-call')

Review comment (suggestion):
The problem occurs not only with `nested routes` but also in the following case:

```ts
const app = new Hono({ router: new TrieRouter() })
app.get('/foo/:id', (c) => c.text(c.req.param('id')))

// GET /foo/keys
```

So, you might not want to write the test in this section. I think a better place could be under `param and query`.

Code after:

    const book = app.basePath('/book')
    book.get('/', (c) => c.text('get /book'))
    book.get('/:id', (c) => {
      return c.text('get /book/' + c.req.param('id'))
    })
    book.post('/', (c) => c.text('post /book'))

    const user = app.basePath('/user')
    user.get('/login', (c) => c.text('get /user/login'))
    user.post('/register', (c) => c.text('post /user/register'))

    const appForEachUser = user.basePath(':id')
    appForEachUser.get('/profile', (c) => c.text('get /user/' + c.req.param('id') + '/profile'))

    app.get('/add-path-after-route-call', (c) => c.text('get /add-path-after-route-call'))

    let res = await app.request('http://localhost/book', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /book')

    res = await app.request('http://localhost/book/123', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /book/123')

    res = await app.request('http://localhost/book', { method: 'POST' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('post /book')

    res = await app.request('http://localhost/book/', { method: 'GET' })
    expect(res.status).toBe(404)

    res = await app.request('http://localhost/user/login', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /user/login')

    res = await app.request('http://localhost/user/register', { method: 'POST' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('post /user/register')

    res = await app.request('http://localhost/user/123/profile', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /user/123/profile')

    res = await app.request('http://localhost/add-path-after-route-call', { method: 'GET' })
    expect(res.status).toBe(200)
    expect(await res.text()).toBe('get /add-path-after-route-call')
  })

  it('Nested route - subApp with basePath', async () => {
    const app = new Hono()