-
-
Notifications
You must be signed in to change notification settings - Fork 738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RegExp with global flag does not match the request path #2134
RegExp with global flag does not match the request path #2134
Comments
Interesting find. Thanks @nivsherf. The issue with global flags in this case is that they maintain state. MDN docs for
Which leads to this fun bit of JS quirkiness: const reg = /repos.*/g;
const str = '/repos/atom/atom/license';
console.log(reg.test(str), reg.test(str), reg.test(str), reg.test(str));
> true false true false While it isn't sensical for the regexp instance to have a global flag in this use case, I do consider it a bug that Nock isn't consistent with its behavior. Throwing an error would be an option, but so would setting the I have a patch written already, PR inbound... |
When matching via a regexp instance, consistently test the entire target string. Sets the `lastIndex` to zero for cases where the `global` flag is set and the regexp instance has been tested previously. fixes: nock#2134
When matching via a regexp instance, consistently test the entire target string. Sets the `lastIndex` to zero for cases where the `global` flag is set and the regexp instance has been tested previously. fixes: nock#2134
When matching via a regexp instance, consistently test the entire target string. Sets the `lastIndex` to zero for cases where the `global` flag is set and the regexp instance has been tested previously. fixes: #2134
🎉 This issue has been resolved in version 13.0.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What is the expected behavior?
When using a RegExp in the path, I expect nock to match regardless of if the RegExp has the global flag set.
What is the actual behavior?
A global RegExp does not match even when it should.
Possible solution
Since I can see no reason why would someone need global matching here (though it was actually used in a codebase I'm working on, and broke after upgrading nock), and this is broken for quite some time (git bisect shows that the offending commit is c1f7082 from July 19), I suggest throwing an explicit error whenever someone tries to pass a RegExp with the global flag.
If you think that this is a reasonable solution, I will submit a PR.
How to reproduce the issue
Here is the script I used with
git bisect
Does the bug have a test case?
Versions
The text was updated successfully, but these errors were encountered: