ftmocks-utils 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,4 +1,6 @@
1
+ // import fs from 'fs';
1
2
  const fs = require('fs')
3
+ // import path from 'path';
2
4
  const path = require('path')
3
5
 
4
6
  const nameToFolder = name => {
@@ -145,14 +147,20 @@ function compareMockToRequest(mock, req) {
145
147
  }
146
148
 
147
149
  function compareMockToFetchRequest(mock, fetchReq) {
148
- const mockURL = processURL(mock.fileContent.url, mock.fileContent.ignoreParams);
149
- const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
150
- const postData = mock.fileContent.request?.postData?.text ? JSON.parse(mock.fileContent.request?.postData?.text) : mock.fileContent.request?.postData;
151
- return isSameRequest({url: mockURL, method: mock.fileContent.method, postData}, {
152
- method: fetchReq.options.method || 'GET',
153
- postData: fetchReq.options.body?.length ? JSON.parse(fetchReq.options.body) : fetchReq.options.body,
154
- url: reqURL,
155
- });
150
+ try{
151
+ const mockURL = processURL(mock.fileContent.url, mock.fileContent.ignoreParams);
152
+ const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
153
+ const postData = mock.fileContent.request?.postData?.text ? JSON.parse(mock.fileContent.request?.postData?.text) : mock.fileContent.request?.postData;
154
+ return isSameRequest({url: mockURL, method: mock.fileContent.method, postData}, {
155
+ method: fetchReq.options.method || 'GET',
156
+ postData: fetchReq.options.body?.length ? JSON.parse(fetchReq.options.body) : fetchReq.options.body,
157
+ url: reqURL,
158
+ });
159
+ } catch(e) {
160
+ console.debug('error at compareMockToFetchRequest', mock, fetchReq);
161
+ console.debug(e);
162
+ }
163
+ return false;
156
164
  }
157
165
 
158
166
  function getMatchingMockData({testMockData, defaultMockData, url, options, testConfig, testName}) {
@@ -190,11 +198,56 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
190
198
  }
191
199
  }
192
200
 
201
+ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName) {
202
+ const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
+ resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
204
+ const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
205
+ console.debug('calling initiatePlaywrightRoutes fetch');
206
+ await page.route('**/*', async (route, request) => {
207
+ const url = request.url();
208
+ const options = {
209
+ options: {
210
+ url,
211
+ method: request.method(),
212
+ body: request.postData(),
213
+ }
214
+ }
215
+ console.debug('got fetch request', request.method(), request.url(), request.postData());
216
+ let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
217
+ if (mockData) {
218
+ console.debug('mocked', url, options);
219
+ } else {
220
+ console.debug('missing mock data', url, options);
221
+ return route.fulfill({
222
+ status: 404,
223
+ headers: new Map([['content-type', 'application/json']]),
224
+ json: () => Promise.resolve({ error: 'Mock data not found' }),
225
+ });
226
+ }
227
+
228
+ const { content, headers, status } = mockData.response;
229
+
230
+ const json = {
231
+ status,
232
+ headers,
233
+ json: () => Promise.resolve(JSON.parse(content)),
234
+ };
235
+
236
+ if(json) {
237
+ await route.fulfill(json);
238
+ } else {
239
+ await route.fallback();
240
+ }
241
+ });
242
+ }
243
+
193
244
  async function initiateJestFetch (jest, ftmocksConifg, testName) {
194
245
  const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
195
246
  resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
196
247
  const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
248
+ console.debug('calling initiateJestFetch fetch');
197
249
  global.fetch = jest.fn((url, options = {}) => {
250
+ console.debug('got fetch request', url, options);
198
251
  let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
199
252
  if (mockData) {
200
253
  console.debug('mocked', url, options);
@@ -216,6 +269,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
216
269
  });
217
270
  });
218
271
 
272
+ console.debug('calling XMLHttpRequest fetch');
219
273
  global.XMLHttpRequest = jest.fn(function () {
220
274
  const xhrMock = {
221
275
  open: jest.fn(),
@@ -288,6 +342,46 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
288
342
  return;
289
343
  };
290
344
 
345
+ function initiateConsoleLogs(jest, ftmocksConifg, testName) {
346
+ const logsFile = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_logs.json');
347
+ let logs = [];
348
+ if(!fs.existsSync(logsFile)) {
349
+ fs.appendFileSync(logsFile, '[]', 'utf8');
350
+ } else {
351
+ fs.writeFileSync(logsFile, '[]', 'utf8');
352
+ }
353
+
354
+ const writeToFile = (type, params) => {
355
+ const logMessage = params.join(' ') + '\n'; // Combine params into a string with spaces
356
+ logs.push({
357
+ type,
358
+ message: logMessage,
359
+ time: Date.now()
360
+ });
361
+ fs.writeFileSync(logsFile, JSON.stringify(logs, null, 2), 'utf8'); // Append the log message to the file
362
+ }
363
+
364
+ global.console = {
365
+ ...console,
366
+ // uncomment to ignore a specific log level
367
+ log: jest.fn((...params) => {
368
+ writeToFile('log', params);
369
+ }),
370
+ debug: jest.fn((...params) => {
371
+ writeToFile('debug', params);
372
+ }),
373
+ info: jest.fn((...params) => {
374
+ writeToFile('info', params);
375
+ }),
376
+ warn: jest.fn((...params) => {
377
+ writeToFile('warn', params);
378
+ }),
379
+ error: jest.fn((...params) => {
380
+ writeToFile('error', params);
381
+ }),
382
+ };
383
+ }
384
+
291
385
 
292
386
  function countFilesInDirectory(directoryPath) {
293
387
  return new Promise((resolve, reject) => {
@@ -329,6 +423,14 @@ const deleteAllSnaps = async (ftmocksConifg, testName) => {
329
423
  fs.rmSync(snapFolder, { recursive: true, force: true });
330
424
  };
331
425
 
426
+ const deleteAllLogs = async (ftmocksConifg, testName) => {
427
+ const mockDir = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`);
428
+ const logFilePath = path.join(mockDir, `_logs.json`);
429
+ fs.rmSync(logFilePath, { recursive: true, force: true });
430
+ };
431
+
432
+
433
+
332
434
  // Export functions as a module
333
435
  module.exports = {
334
436
  compareMockToRequest,
@@ -342,5 +444,8 @@ module.exports = {
342
444
  resetAllMockStats,
343
445
  initiateJestFetch,
344
446
  saveSnap,
345
- deleteAllSnaps
447
+ deleteAllSnaps,
448
+ deleteAllLogs,
449
+ initiateConsoleLogs,
450
+ initiatePlaywrightRoutes
346
451
  };
package/src/recorder.js CHANGED
@@ -12,7 +12,7 @@ window.FTMOCKS_CONFIG = {
12
12
  const addTrack = track => {
13
13
  track.id = recordedTracks.length ? recordedTracks[recordedTracks.length - 1].id + 1 : 1;
14
14
  track.time = new Date();
15
- track.bodyHtml = document.documentElement.outerHTML.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');;
15
+ // track.bodyHtml = document.documentElement.outerHTML.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');;
16
16
 
17
17
  fetch(window.FTMOCKS_CONFIG.record_events_url, {
18
18
  method: 'POST',