Skip to content

Commit 89890b6

Browse files
committed
feat: remove debug mode from fetch-mock
1 parent 0fb5c9f commit 89890b6

File tree

10 files changed

+479
-969
lines changed

10 files changed

+479
-969
lines changed

package-lock.json

Lines changed: 476 additions & 732 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fetch-mock/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
},
4343
"homepage": "http://www.wheresrhys.co.uk/fetch-mock",
4444
"dependencies": {
45-
"debug": "^4.1.1",
4645
"dequal": "^2.0.3",
4746
"globrex": "^0.1.2",
4847
"is-subset": "^0.1.1",

packages/fetch-mock/src/Route/index.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import builtInMatchers from './matchers.js';
2-
import { debug, setDebugNamespace, getDebug } from '../lib/debug.js';
32

43
const isUrlMatcher = (matcher) =>
54
matcher instanceof RegExp ||
@@ -14,8 +13,6 @@ const nameToOptions = (options) =>
1413
class Route {
1514
constructor(args, fetchMock) {
1615
this.fetchMock = fetchMock;
17-
const debug = getDebug('compileRoute()');
18-
debug('Compiling route');
1916
this.init(args);
2017
this.sanitize();
2118
this.validate();
@@ -63,32 +60,21 @@ class Route {
6360
}
6461

6562
sanitize() {
66-
const debug = getDebug('sanitize()');
67-
debug('Sanitizing route properties');
6863

6964
if (this.method) {
70-
debug(`Converting method ${this.method} to lower case`);
7165
this.method = this.method.toLowerCase();
7266
}
7367
if (isUrlMatcher(this.matcher)) {
74-
debug('Mock uses a url matcher', this.matcher);
7568
this.url = this.matcher;
7669
delete this.matcher;
7770
}
7871

7972
this.functionMatcher = this.matcher || this.functionMatcher;
8073

81-
debug('Setting route.identifier...');
82-
debug(` route.name is ${this.name}`);
83-
debug(` route.url is ${this.url}`);
84-
debug(` route.functionMatcher is ${this.functionMatcher}`);
8574
this.identifier = this.name || this.url || this.functionMatcher;
86-
debug(` -> route.identifier set to ${this.identifier}`);
8775
}
8876

8977
generateMatcher() {
90-
setDebugNamespace('generateMatcher()');
91-
debug('Compiling matcher for route');
9278

9379
const activeMatchers = Route.registeredMatchers
9480
.map(
@@ -99,23 +85,15 @@ class Route {
9985

10086
this.usesBody = activeMatchers.some(({ usesBody }) => usesBody);
10187

102-
debug('Compiled matcher for route');
103-
setDebugNamespace();
10488
this.matcher = (url, options = {}, request) =>
10589
activeMatchers.every(({ matcher }) => matcher(url, options, request));
10690
}
10791

10892
limit() {
109-
const debug = getDebug('limit()');
110-
debug('Limiting number of requests to handle by route');
11193
if (!this.repeat) {
112-
debug(
113-
' No `repeat` value set on route. Will match any number of requests',
114-
);
11594
return;
11695
}
11796

118-
debug(` Route set to repeat ${this.repeat} times`);
11997
const { matcher } = this;
12098
let timesLeft = this.repeat;
12199
this.matcher = (url, options) => {
@@ -131,21 +109,13 @@ class Route {
131109
}
132110

133111
delayResponse() {
134-
const debug = getDebug('delayResponse()');
135-
debug('Applying response delay settings');
136112
if (this.delay) {
137-
debug(` Wrapping response in delay of ${this.delay} miliseconds`);
138113
const { response } = this;
139114
this.response = () => {
140-
debug(`Delaying response by ${this.delay} miliseconds`);
141115
return new Promise((res) =>
142116
setTimeout(() => res(response), this.delay),
143117
);
144118
};
145-
} else {
146-
debug(
147-
" No delay set on route. Will respond 'immediately' (but asynchronously)",
148-
);
149119
}
150120
}
151121

packages/fetch-mock/src/Route/matchers.js

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {
88
getQuery,
99
normalizeUrl,
1010
} from '../lib/request-utils.js';
11-
import { debug } from '../lib/debug.js';
1211

1312
const debuggableUrlFunc = (func) => (url) => {
14-
debug('Actual url:', url);
1513
return func(url);
1614
};
1715

@@ -35,46 +33,32 @@ const stringMatchers = {
3533
};
3634

3735
const getHeaderMatcher = ({ headers: expectedHeaders }) => {
38-
debug('Generating header matcher');
3936
if (!expectedHeaders) {
40-
debug(' No header expectations defined - skipping');
4137
return;
4238
}
4339
const expectation = headerUtils.toLowerCase(expectedHeaders);
44-
debug(' Expected headers:', expectation);
4540
return (url, { headers = {} }) => {
46-
debug('Attempting to match headers');
4741
const lowerCaseHeaders = headerUtils.toLowerCase(
4842
headerUtils.normalize(headers),
4943
);
50-
debug(' Expected headers:', expectation);
51-
debug(' Actual headers:', lowerCaseHeaders);
5244
return Object.keys(expectation).every((headerName) =>
5345
headerUtils.equal(lowerCaseHeaders[headerName], expectation[headerName]),
5446
);
5547
};
5648
};
5749

5850
const getMethodMatcher = ({ method: expectedMethod }) => {
59-
debug('Generating method matcher');
6051
if (!expectedMethod) {
61-
debug(' No method expectations defined - skipping');
6252
return;
6353
}
64-
debug(' Expected method:', expectedMethod);
6554
return (url, { method }) => {
66-
debug('Attempting to match method');
6755
const actualMethod = method ? method.toLowerCase() : 'get';
68-
debug(' Expected method:', expectedMethod);
69-
debug(' Actual method:', actualMethod);
7056
return expectedMethod === actualMethod;
7157
};
7258
};
7359

7460
const getQueryStringMatcher = ({ query: passedQuery }) => {
75-
debug('Generating query parameters matcher');
7661
if (!passedQuery) {
77-
debug(' No query parameters expectations defined - skipping');
7862
return;
7963
}
8064

@@ -101,14 +85,8 @@ const getQueryStringMatcher = ({ query: passedQuery }) => {
10185

10286
const keys = Array.from(expectedQuery.keys());
10387
return (url) => {
104-
debug('Attempting to match query parameters');
10588
const queryString = getQuery(url);
10689
const query = new URLSearchParams(queryString);
107-
debug(
108-
' Expected query parameters:',
109-
Object.fromEntries(expectedQuery.entries()),
110-
);
111-
debug(' Actual query parameters:', Object.fromEntries(query.entries()));
11290

11391
return keys.every((key) => {
11492
const expectedValues = expectedQuery.getAll(key).sort();
@@ -130,30 +108,24 @@ const getQueryStringMatcher = ({ query: passedQuery }) => {
130108
};
131109

132110
const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => {
133-
debug('Generating path parameters matcher');
134111
if (!expectedParams) {
135-
debug(' No path parameters expectations defined - skipping');
136112
return;
137113
}
138114
if (!/express:/.test(matcherUrl)) {
139115
throw new Error(
140116
'fetch-mock: matching on params is only possible when using an express: matcher',
141117
);
142118
}
143-
debug(' Expected path parameters:', expectedParams);
144119
const expectedKeys = Object.keys(expectedParams);
145120
const re = regexparam.parse(matcherUrl.replace(/^express:/, ''));
146121
return (url) => {
147-
debug('Attempting to match path parameters');
148122
const vals = re.pattern.exec(getPath(url)) || [];
149123
vals.shift();
150124
const params = re.keys.reduce(
151125
(map, paramName, i) =>
152126
vals[i] ? Object.assign(map, { [paramName]: vals[i] }) : map,
153127
{},
154128
);
155-
debug(' Expected path parameters:', expectedParams);
156-
debug(' Actual path parameters:', params);
157129
return expectedKeys.every((key) => params[key] === expectedParams[key]);
158130
};
159131
};
@@ -162,28 +134,17 @@ const getBodyMatcher = (route, fetchMock) => {
162134
const matchPartialBody = fetchMock.getOption('matchPartialBody', route);
163135
const { body: expectedBody } = route;
164136

165-
debug('Generating body matcher');
166137
return (url, { body, method = 'get' }) => {
167-
debug('Attempting to match body');
168138
if (method.toLowerCase() === 'get') {
169-
debug(' GET request - skip matching body');
170139
// GET requests don’t send a body so the body matcher should be ignored for them
171140
return true;
172141
}
173142

174143
let sentBody;
175144

176145
try {
177-
debug(' Parsing request body as JSON');
178146
sentBody = JSON.parse(body);
179-
} catch (err) {
180-
debug(' Failed to parse request body as JSON', err);
181-
}
182-
debug('Expected body:', expectedBody);
183-
debug('Actual body:', sentBody);
184-
if (matchPartialBody) {
185-
debug('matchPartialBody is true - checking for partial match only');
186-
}
147+
} catch {} // eslint-disable-line no-empty
187148

188149
return (
189150
sentBody &&
@@ -199,55 +160,42 @@ const getFullUrlMatcher = (route, matcherUrl, query) => {
199160
// but we have to be careful to normalize the url we check and the name
200161
// of the route to allow for e.g. http://it.at.there being indistinguishable
201162
// from http://it.at.there/ once we start generating Request/Url objects
202-
debug(' Matching using full url', matcherUrl);
203163
const expectedUrl = normalizeUrl(matcherUrl);
204-
debug(' Normalised url to:', matcherUrl);
205164
if (route.identifier === matcherUrl) {
206-
debug(' Updating route identifier to match normalized url:', matcherUrl);
207165
route.identifier = expectedUrl;
208166
}
209167

210168
return (matcherUrl) => {
211-
debug('Expected url:', expectedUrl);
212-
debug('Actual url:', matcherUrl);
213169
if (query && expectedUrl.indexOf('?')) {
214-
debug('Ignoring query string when matching url');
215170
return matcherUrl.indexOf(expectedUrl) === 0;
216171
}
217172
return normalizeUrl(matcherUrl) === expectedUrl;
218173
};
219174
};
220175

221176
const getFunctionMatcher = ({ functionMatcher }) => {
222-
debug('Detected user defined function matcher', functionMatcher);
223177
return (...args) => {
224-
debug('Calling function matcher with arguments', args);
225178
return functionMatcher(...args);
226179
};
227180
};
228181

229182
const getUrlMatcher = (route) => {
230-
debug('Generating url matcher');
231183
const { url: matcherUrl, query } = route;
232184

233185
if (matcherUrl === '*') {
234-
debug(' Using universal * rule to match any url');
235186
return () => true;
236187
}
237188

238189
if (matcherUrl instanceof RegExp) {
239-
debug(' Using regular expression to match url:', matcherUrl);
240190
return (url) => matcherUrl.test(url);
241191
}
242192

243193
if (matcherUrl.href) {
244-
debug(' Using URL object to match url', matcherUrl);
245194
return getFullUrlMatcher(route, matcherUrl.href, query);
246195
}
247196

248197
for (const shorthand in stringMatchers) {
249198
if (matcherUrl.indexOf(`${shorthand}:`) === 0) {
250-
debug(` Using ${shorthand}: pattern to match url`, matcherUrl);
251199
const urlFragment = matcherUrl.replace(new RegExp(`^${shorthand}:`), '');
252200
return stringMatchers[shorthand](urlFragment);
253201
}

packages/fetch-mock/src/lib/debug.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)