Skip to content

Commit

Permalink
fix: empty routes tree printing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed May 9, 2023
1 parent c915678 commit c7464f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ Router.prototype.prettyPrint = function (options = {}) {

options.buildPrettyMeta = this.buildPrettyMeta.bind(this)

let tree = null
if (method === undefined) {
const { version, host, ...constraints } = this.constrainer.strategies
constraints[httpMethodStrategy.name] = httpMethodStrategy
Expand All @@ -574,11 +575,12 @@ Router.prototype.prettyPrint = function (options = {}) {
return { ...route, method: 'MERGED', opts: { constraints } }
})
mergedRouter._rebuild(mergedRoutes)

return prettyPrintTree(mergedRouter.trees.MERGED, options)
tree = mergedRouter.trees.MERGED
} else {
tree = this.trees[method]
}

const tree = this.trees[method]
if (tree == null) return '(empty tree)'
return prettyPrintTree(tree, options)
}

Expand Down
11 changes: 11 additions & 0 deletions test/pretty-print-tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const t = require('tap')
const test = t.test
const FindMyWay = require('../')

test('pretty print - empty tree', t => {
t.plan(2)

const findMyWay = FindMyWay()
const tree = findMyWay.prettyPrint({ method: 'GET' })

const expected = '(empty tree)'
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})

test('pretty print - static routes', t => {
t.plan(2)

Expand Down
11 changes: 11 additions & 0 deletions test/pretty-print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const t = require('tap')
const test = t.test
const FindMyWay = require('../')

test('pretty print - empty tree', t => {
t.plan(2)

const findMyWay = FindMyWay()
const tree = findMyWay.prettyPrint()

const expected = '(empty tree)'
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})

test('pretty print - static routes', t => {
t.plan(2)

Expand Down

0 comments on commit c7464f6

Please sign in to comment.