jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

Global modifier (\g) in regex parameter brings the match to a fail #30

Closed dfedynich closed 7 years ago

dfedynich commented 7 years ago

Hello, team.

What's Up

I want to match the set of URLs, which are different with query parameters only. To achieve this goal, I pass regex instead of string to mock.get as the first parameter according to your specification .get(url | regex, fn) This approach misses the passed URL one time in a set of two requests if the regex value contains the global modifier (\g).

Example

var mock = require('xhr-mock');
mock.setup();

mock.get(/(api\/users).*/g, function(req, res) {
  return res.status(200).body('<h1>Google</h1>');
});

// ---------

var axios = require('axios');
setInterval(function() {
    axios({url: 'api/users'}).then(
  function(res) {
    console.log('loaded', res.data);
  },
  function(error) {
    console.log('ERROR', error);
  }
);
}, 2000);

As the result, we will get the error at every second request. However, if we remove the global modifier from the regex, mock will start to work as expected.

jameslnewell commented 7 years ago

"team" 🤣 I wish.

Try 1.9.1.