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 26, 2023
1 parent 537e97f commit 83b4830
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/route.js
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 @@ -1021,7 +1020,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 @@ -1095,6 +1094,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 83b4830

Please sign in to comment.