Skip to content

Commit cd57904

Browse files
committed
fix(warn): added warning if matcherUrl contains queryparams
1 parent 81fb819 commit cd57904

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/matcher-utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { HttpMethod, MatcherUrl, RequestUrl, RouteMatcher } from './types';
22
import { findRequestMethod, findRequestUrl } from './internal-utils';
3-
const pathToRegex = require('path-to-regexp');
43
import { Key } from 'path-to-regexp';
54

5+
const pathToRegex = require('path-to-regexp');
6+
7+
const heuristics = [/\?\w=\w/, /\&\w=\w/, /\?\w$/, /\&\w$/];
8+
function containsQueryParams(matcherUrl: string): boolean {
9+
if (matcherUrl.includes('?')) {
10+
return heuristics.some(heuristic => heuristic.test(matcherUrl));
11+
}
12+
return false;
13+
}
14+
615
function httpMethodHelper(matcherUrl: MatcherUrl, httpMethod: HttpMethod): RouteMatcher {
716
if (typeof matcherUrl === 'string') {
817
return MatcherUtils.combine(MatcherUtils.method(httpMethod), MatcherUtils.url(matcherUrl));
@@ -30,6 +39,23 @@ export default class MatcherUtils {
3039
}
3140

3241
static url(matcherUrl: string): RouteMatcher {
42+
if (containsQueryParams(matcherUrl)) {
43+
console.warn(
44+
`
45+
Matching url '${matcherUrl}' seems to contain queryparameters.
46+
This is unfortunatly not supported due to a limitation in the matching library.
47+
48+
If the mock-response is dependent on the queryparameter you must use the following;
49+
50+
mock.get('/path-without-queryparam', ({ queryParams }) => {
51+
if (queryParams.paramName === 'paramValue') {
52+
return mockDataGivenParam;
53+
}
54+
return mockDataWithoutParam;
55+
});
56+
`.trim()
57+
);
58+
}
3359
return {
3460
test: (input: RequestInfo, init?: RequestInit) => {
3561
if (matcherUrl === '*') {

0 commit comments

Comments
 (0)