cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Fix optional laravel route parameters

Open whitespacecode opened this issue 3 years ago • 2 comments

Update laravel-routes.js to fix having optional route parameters

whitespacecode avatar Dec 14 '22 13:12 whitespacecode

Fix for optional parameters in the laravel route "/teams/{team}/dashboard/{style?}"

uri = uri.replace(
     new RegExp(`{${parameter}[^}]*}`),
     parameters[parameter]
);

In this example, the regular expression /{style[^}]*}/ will match {style} or {style?} in the string variable. The [^}] part of the regular expression matches any character that is not a closing curly bracket (}), and the * character matches zero or more of the preceding character or group. This allows the regular expression to match any characters between the {style and } parts of the string.

whitespacecode avatar Dec 14 '22 13:12 whitespacecode

I just ran into this myself, but the regex I came up with is a little different. The problem I see with the one proposed here is that it will also match for example {styleName}. Here's my version, which only adds support for an optional ? after the parameter name: https://github.com/laracasts/cypress/pull/83

devnll avatar Jul 22 '23 22:07 devnll