Skip to content

Commit

Permalink
tests: use feature detetion for named capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 25, 2023
1 parent 3c03f58 commit b72f040
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var shouldHitHandle = utils.shouldHitHandle
var shouldNotHaveBody = utils.shouldNotHaveBody
var shouldNotHitHandle = utils.shouldNotHitHandle

// Named capturing groups are available from Node `10.0.0` and up
var describeCaptureGroups = runningOnNodeMajorVersionGreaterOrEqualThan(10) ? describe : describe.skip
var describeNamedCaptureGroups = supportsRegExp('(?<foo>.)') ? describe : describe.skip
var describePromises = global.Promise ? describe : describe.skip

describe('Router', function () {
Expand Down Expand Up @@ -983,7 +982,7 @@ describe('Router', function () {
})
})

describeCaptureGroups('using "(?<name>)"', function () {
describeNamedCaptureGroups('using "(?<name>)"', function () {
it('should allow defining capturing groups using regexps', function (done) {
var cb = after(3, done)
var router = new Router()
Expand Down Expand Up @@ -1057,6 +1056,10 @@ function sendParams (req, res) {
res.end(JSON.stringify(req.params))
}

function runningOnNodeMajorVersionGreaterOrEqualThan (version) {
return Number(process.versions.node.split('.')[0]) >= Number(version)
function supportsRegExp (source) {
try {
return RegExp(source).source !== ''
} catch (e) {
return false
}
}

0 comments on commit b72f040

Please sign in to comment.