ftmocks-utils 1.0.6 → 1.0.7

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.6",
3
+ "version": "1.0.7",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,10 +1,17 @@
1
- import fs from 'fs';
2
- import path from 'path';
1
+ const fs = require('fs')
2
+ const path = require('path')
3
3
 
4
- export const nameToFolder = name => {
4
+ const nameToFolder = name => {
5
5
  return name.replaceAll(' ', '_');
6
6
  };
7
7
 
8
+ const getMockDir = config => {
9
+ if(!path.isAbsolute(config.MOCK_DIR)) {
10
+ return path.resolve( process.cwd(), config.MOCK_DIR);
11
+ }
12
+ return config.MOCK_DIR;
13
+ }
14
+
8
15
  const areJsonEqual = (jsonObj1, jsonObj2) => {
9
16
  // Check if both are objects and not null
10
17
  if (typeof jsonObj1 === 'object' && jsonObj1 !== null &&
@@ -34,7 +41,7 @@ const areJsonEqual = (jsonObj1, jsonObj2) => {
34
41
  }
35
42
 
36
43
  const getDefaultMockDataFromConfig = (testConfig) => {
37
- const defaultPath = path.join(testConfig.MOCK_DIR, 'default.json');
44
+ const defaultPath = path.join(getMockDir(testConfig), 'default.json');
38
45
 
39
46
  try {
40
47
  const defaultData = fs.readFileSync(defaultPath, 'utf8');
@@ -42,7 +49,7 @@ try {
42
49
 
43
50
  // Read and attach mock data for each entry in parsedData
44
51
  parsedData.forEach(entry => {
45
- const mockFilePath = path.join(testConfig.MOCK_DIR, 'defaultMocks', `mock_${entry.id}.json`);;
52
+ const mockFilePath = path.join(getMockDir(testConfig), 'defaultMocks', `mock_${entry.id}.json`);;
46
53
  try {
47
54
  const mockData = fs.readFileSync(mockFilePath, 'utf8');
48
55
  entry.fileContent = JSON.parse(mockData);
@@ -64,18 +71,18 @@ const loadMockDataFromConfig = (testConfig, _testName) => {
64
71
  let testName = _testName;
65
72
  if(!testName) {
66
73
  // Read the test ID from mockServer.config.json
67
- const configPath = path.join(testConfig.MOCK_DIR, 'mockServer.config.json');
74
+ const configPath = path.join(getMockDir(testConfig), 'mockServer.config.json');
68
75
  const configData = fs.readFileSync(configPath, 'utf8');
69
76
  const config = JSON.parse(configData);
70
77
  testName = config.testName;
71
78
  }
72
79
  // Read the tests from testConfig
73
- const mocksPath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, '_mock_list.json');
80
+ const mocksPath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, '_mock_list.json');
74
81
  const mocksData = fs.readFileSync(mocksPath, 'utf8');
75
82
  const mocks = JSON.parse(mocksData);
76
83
 
77
84
  mocks.forEach(mock => {
78
- const fileContent = JSON.parse(fs.readFileSync(path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${mock.id}.json`), 'utf8'));
85
+ const fileContent = JSON.parse(fs.readFileSync(path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${mock.id}.json`), 'utf8'));
79
86
  mock.fileContent = fileContent;
80
87
  });
81
88
 
@@ -160,7 +167,7 @@ function getMatchingMockData({testMockData, defaultMockData, url, options, testC
160
167
  let foundMock = matchedMocks.find(mock => !mock.fileContent.served) ? matchedMocks.find(mock => !mock.fileContent.served) : matchedMocks[matchedMocks.length - 1];
161
168
  // updating stats to mock file
162
169
  if(foundMock) {
163
- const mockFilePath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${foundMock.id}.json`);
170
+ const mockFilePath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${foundMock.id}.json`);
164
171
  foundMock.fileContent.served = true;
165
172
  fs.writeFileSync(mockFilePath, JSON.stringify(foundMock.fileContent, null, 2));
166
173
  }
@@ -177,7 +184,7 @@ function getMatchingMockData({testMockData, defaultMockData, url, options, testC
177
184
  async function resetAllMockStats({testMockData, testConfig, testName}) {
178
185
  for(let i=0; i<testMockData.length; i++) {
179
186
  const tmd = testMockData[i];
180
- const mockFilePath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${tmd.id}.json`);
187
+ const mockFilePath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${tmd.id}.json`);
181
188
  tmd.fileContent.served = false;
182
189
  await fs.writeFileSync(mockFilePath, JSON.stringify(tmd.fileContent, null, 2));
183
190
  }
@@ -301,8 +308,8 @@ function countFilesInDirectory(directoryPath) {
301
308
  }
302
309
 
303
310
  const saveSnap = async (html, ftmocksConifg, testName) => {
304
- const snapFolder = path.join(ftmocksConifg.MOCK_DIR, `test_${nameToFolder(testName)}`, '_snaps');
305
- const snapTemplate = path.join(ftmocksConifg.MOCK_DIR, 'snap_template.html');
311
+ const snapFolder = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_snaps');
312
+ const snapTemplate = path.join(getMockDir(ftmocksConifg), 'snap_template.html');
306
313
 
307
314
  if (!fs.existsSync(snapFolder)) {
308
315
  fs.mkdirSync(snapFolder);
@@ -318,7 +325,7 @@ const saveSnap = async (html, ftmocksConifg, testName) => {
318
325
  };
319
326
 
320
327
  const deleteAllSnaps = async (ftmocksConifg, testName) => {
321
- const snapFolder = path.join(ftmocksConifg.MOCK_DIR, `test_${nameToFolder(testName)}`, '_snaps');
328
+ const snapFolder = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_snaps');
322
329
  fs.rmSync(snapFolder, { recursive: true, force: true });
323
330
  };
324
331
 
package/src/recorder.js CHANGED
@@ -1,3 +1,9 @@
1
+ window.FTMOCKS_CONFIG = {
2
+ record_mocks_url: 'http://localhost:5000/api/v1/recordMockdata',
3
+ record_events_url: 'http://localhost:5000/api/v1/recordedEvents'
4
+ };
5
+
6
+
1
7
  (function () {
2
8
  // Intercept Fetch API
3
9
  const originalFetch = window.fetch;