ftmocks-utils 1.0.7 → 1.0.8

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.8",
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}) {
@@ -194,7 +202,9 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
194
202
  const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
195
203
  resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
196
204
  const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
205
+ console.debug('calling initiateJestFetch fetch');
197
206
  global.fetch = jest.fn((url, options = {}) => {
207
+ console.debug('got fetch request', url, options);
198
208
  let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
199
209
  if (mockData) {
200
210
  console.debug('mocked', url, options);
@@ -216,6 +226,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
216
226
  });
217
227
  });
218
228
 
229
+ console.debug('calling XMLHttpRequest fetch');
219
230
  global.XMLHttpRequest = jest.fn(function () {
220
231
  const xhrMock = {
221
232
  open: jest.fn(),
@@ -288,6 +299,46 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
288
299
  return;
289
300
  };
290
301
 
302
+ function initiateConsoleLogs(jest, ftmocksConifg, testName) {
303
+ const logsFile = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_logs.json');
304
+ let logs = [];
305
+ if(!fs.existsSync(logsFile)) {
306
+ fs.appendFileSync(logsFile, '[]', 'utf8');
307
+ } else {
308
+ fs.writeFileSync(logsFile, '[]', 'utf8');
309
+ }
310
+
311
+ const writeToFile = (type, params) => {
312
+ const logMessage = params.join(' ') + '\n'; // Combine params into a string with spaces
313
+ logs.push({
314
+ type,
315
+ message: logMessage,
316
+ time: Date.now()
317
+ });
318
+ fs.writeFileSync(logsFile, JSON.stringify(logs, null, 2), 'utf8'); // Append the log message to the file
319
+ }
320
+
321
+ global.console = {
322
+ ...console,
323
+ // uncomment to ignore a specific log level
324
+ log: jest.fn((...params) => {
325
+ writeToFile('log', params);
326
+ }),
327
+ debug: jest.fn((...params) => {
328
+ writeToFile('debug', params);
329
+ }),
330
+ info: jest.fn((...params) => {
331
+ writeToFile('info', params);
332
+ }),
333
+ warn: jest.fn((...params) => {
334
+ writeToFile('warn', params);
335
+ }),
336
+ error: jest.fn((...params) => {
337
+ writeToFile('error', params);
338
+ }),
339
+ };
340
+ }
341
+
291
342
 
292
343
  function countFilesInDirectory(directoryPath) {
293
344
  return new Promise((resolve, reject) => {
@@ -329,6 +380,14 @@ const deleteAllSnaps = async (ftmocksConifg, testName) => {
329
380
  fs.rmSync(snapFolder, { recursive: true, force: true });
330
381
  };
331
382
 
383
+ const deleteAllLogs = async (ftmocksConifg, testName) => {
384
+ const mockDir = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`);
385
+ const logFilePath = path.join(mockDir, `_logs.json`);
386
+ fs.rmSync(logFilePath, { recursive: true, force: true });
387
+ };
388
+
389
+
390
+
332
391
  // Export functions as a module
333
392
  module.exports = {
334
393
  compareMockToRequest,
@@ -342,5 +401,7 @@ module.exports = {
342
401
  resetAllMockStats,
343
402
  initiateJestFetch,
344
403
  saveSnap,
345
- deleteAllSnaps
404
+ deleteAllSnaps,
405
+ deleteAllLogs,
406
+ initiateConsoleLogs
346
407
  };
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',